2017-12-26 6 views
1

완료된 작업과 수행 할 작업이있는 모델이 있습니다. 보기에는 링크가 있고 링크를 클릭하면 작업을 todo에서 완료로 변경해야합니다. 그것은왜 리디렉션이 잘못 되었습니까?

get 'tasks/:id', to: 'tasks#change_to_done', as: 'change_to_done' 

내보기

<% unless task.done %> 
     <td><%= check_box_tag "cb_tasks[]", task.id %></td> 
     <td><%= link_to task.title, task %></td> 
     <td><%= link_to 'Edit', edit_task_path(task.id) %></td> 
     <td><%= link_to 'Done', change_to_done_path(task.id) %></td> 
     <td><%= link_to 'Destroy', task, method: :delete, data: {confirm: 'Are you sure?'} %></td> 
     <% end %> 

내 컨트롤러 내 경로의

def change_to_done 
    @task = Task.find(params[:id]) 
    @task.done = true 
    @task.save 
    end 

쇼 경로

Started GET "/tasks/32" for 127.0.0.1 at 2017-12-26 11:59:06 +0200 
Processing by TasksController#show as HTML 
    Parameters: {"id"=>"32"} 
    Task Load (0.1ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT ? [["id", 32], ["LIMIT", 1]] 
    Rendering tasks/show.html.erb within layouts/application 
    Rendered tasks/show.html.erb within layouts/application (0.7ms) 
Completed 200 OK in 57ms (Views: 55.0ms | ActiveRecord: 0.1ms) 

어떤 문제에 링크 리디렉션을 클릭? 필드 상태를 false에서 true로 변경하면됩니다.

답변

2

레일 경로가 지정된 순서대로 일치하므로 resources :tasks 이상인 경우 'task/:id'을 얻으십시오. 리소스 줄에 대한 show 작업 경로는 get 줄보다 먼저 일치합니다.

예를 들어, 엔드 포인트를 변경하려고 :

get 'change_to_done/:id', to: 'tasks#change_to_done', as: 'change_to_done' 
1

당신은 당신의 루트 파일에 resources :tasks이있을 수 있습니다. 레일스가이 문제를 해결하고 get 'tasks/:id', to: 'tasks#change_to_done', as: 'change_to_done'이 경로를 무시합니다. 정확한 동작을 지정해야합니다. get 'tasks/:id/change_to_done', to: 'tasks#change_to_done', as: 'change_to_done'

관련 문제