1

在我的 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

像这样的错误是否会导致此问题:

在此处输入图像描述

4

2 回答 2

1

我相信您正在尝试为单个资源 ProjectTodo “切换状态”,对吗?在 Rails 3 中描述这条路线的更常见的方式如下:

resources :project_todos do
  member do
    post :toggle_status
  end
end

这会在您的路线文件中产生确定性顺序:

toggle_status_project_todo POST   /project_todos/:id/toggle_status(.:format) project_todos#toggle_status
             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

允许您在视图中编写:

<%= link_to 'Toggle Project', toggle_status_project_todo_path(@todo), method: "post" %>

这将发布到“http://localhost:3000/project_todos/1/toggle_status”

于 2012-09-21T16:42:02.197 回答
0

Try moving your project_todos line above the resources: project_todos line in your routes.rb

Then the custom route will be found before the id version.

于 2012-09-21T16:27:31.930 回答