拥有完成任务的模型和待办事项。在视图中有链接,当点击链接时,它必须将任务从待办事项更改为完成。这是我的路线
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。
答案 0 :(得分:2)
Rails路由按照指定的顺序进行匹配,因此如果您的resources :tasks
高于get 'task/:id'
,则资源行的show action路线将在获取之前进行匹配线。
尝试更改端点,例如:
get 'change_to_done/:id', to: 'tasks#change_to_done', as: 'change_to_done'
答案 1 :(得分:1)
您的路线文件中可能包含resources :tasks
。 Rails解决了这个问题,忽略了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'