Skip to content

Commit

Permalink
Add link to update task state
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki-jpn20 committed Jun 13, 2023
1 parent c410fa8 commit 1b41e99
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# coding: utf-8
class TasksController < ApplicationController
before_action :set_task, only: %i[ show edit update destroy ]
before_action :set_task, only: %i[ show edit update destroy complete ]
before_action :get_form_data, only: %i[ new edit ]
before_action :logged_in_user, only: %i[ new create edit update destroy]
before_action :logged_in_user, only: %i[ new create edit update destroy complete ]
before_action :search, only: %i[ index ]

def search
Expand Down Expand Up @@ -67,6 +67,17 @@ def update
end
end

def complete
task_state_name = params[:task_state_name]
@task.task_state_id = TaskState.find_by(name: task_state_name).id
if @task.update(task_state_params)
flash[:success] = "タスクを更新しました"
redirect_to tasks_path
else
redirect_back fallback_location: edit_task_path(@task)
end
end

# DELETE /tasks/1 or /tasks/1.json
def destroy
@task.destroy
Expand Down Expand Up @@ -95,6 +106,10 @@ def task_params
params.require(:task).permit(:assigner_id, :due_at, :content, :description, :project_id, :task_state_id)
end

def task_state_params
params.permit(:task_state_id)
end

def parse_tag_names(tag_names)
@task.tags = tag_names.split.map do |tag_name|
tag = Tag.find_by(name: tag_name)
Expand Down
1 change: 1 addition & 0 deletions app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<%= link_to '詳細', task %>
<%= link_to '編集', edit_task_path(task) %>
<%= link_to '削除', task, method: :delete, data: { confirm: 'このタスクを削除しますか?' } %>
<%= link_to '完了', complete_path(task, task_state_name: "done"), method: :put %>
<% end %>
</div>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
delete '/logout', to: 'sessions#destroy'
get '/login', to: 'sessions#login_with_passwd_auth'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
put '/tasks/:id/complete', to: 'tasks#complete', as: :complete
end

0 comments on commit 1b41e99

Please sign in to comment.