在我的 rake 路线中,我得到:
project_todos GET /project_todos(.:format) project_todos#index
POST /project_todos(.:format) project_todos#create
new_project_todo GET /project_todos/new(.:format) project_todos#new
edit_project_todo GET /project_todos/:id/edit(.:format) project_todos#edit
project_todo GET /project_todos/:id(.:format) project_todos#show
PUT /project_todos/:id(.:format) project_todos#update
DELETE /project_todos/:id(.:format) project_todos#destroy
project_todos_toggle_status POST /project_todos/toggle_status(.:format) project_todos#toggle_status
从:
resources :project_todos
post "project_todos/toggle_status"
我的观点中有一条线,如下所示:
<%= link_to 'Toggle Project', project_todos_toggle_status_path(id: todo.id), method: "post" %>
这会产生一个如下所示的链接:
http://localhost:3000/project_todos/toggle_status?id=1
并导致以下错误:
这很奇怪,因为它进入了 show 操作,我不明白它为什么或如何设置 id = toggle_status.
关于如何完成这项工作的任何想法?我过去使用 static_pages 控制器做过非常相似的事情,这会有什么不同吗?
更新:
开发日志:
Started GET "/project_todos/1/toggle_status" for 127.0.0.1 at 2012-09-21 12:08:09 -0500
ActionController::RoutingError (No route matches [GET] "/project_todos/1/toggle_status"):
actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.8) lib/rails/engine.rb:479:in `call'
railties (3.2.8) lib/rails/application.rb:223:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/local/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms)
路线.rb
resources :project_todos do
member do
post :toggle_status
end
end
查看代码:
<% @todos.each do |todo| %>
<li class="todo_item todo"><%= todo.title %></li>
<%= todo.id%>
<%= link_to 'Toggle Project', toggle_status_project_todo_path(todo), method: "post" %>
<% end %>
应用程序.js
//= require jquery
//= require jquery_ujs
//= require tinymce-jquery
//= require tinymce
//= require_tree .
更新#2
像这样的错误是否会导致此问题: