【问题标题】:Rails resourceful routes all point to index action?Rails 资源丰富的路由都指向索引操作?
【发布时间】:2010-10-15 02:49:19
【问题描述】:

我正在运行 Rails 2.3.8。

我在 routes.rb 文件中设置了map.resources :users

当我运行rake routes 时,它显示:

users GET /users(.:format)                   {:action=>"index", :controller=>"users"}
GET /users(.:format)                   {:action=>"index", :controller=>"users"}
new_user GET /users/new(.:format)               {:action=>"index", :controller=>"users"}
edit_user GET /users/:id/edit(.:format)          {:action=>"index", :controller=>"users"}
user GET /users/:id(.:format)               {:action=>"index", :controller=>"users"}
GET /users/:id(.:format)               {:action=>"index", :controller=>"users"}
GET /users/:id(.:format)               {:action=>"index", :controller=>"users"}
/:controller/:action/:id           
/:controller/:action/:id(.:format)

对格式问题感到抱歉。但重点是...... 1)我的“PUT”、“POST”等路由在哪里?,2)为什么一切都指向index??

任何帮助将不胜感激...谢谢!

更新:完整路线文件:

ActionController::Routing::Routes.draw do |map|
  map.login "login", :controller => "user_sessions", :action => "new"
  map.logout "logout", :controller => "user_sessions", :action => "destroy"

  map.resources :users

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

我的 users_controller 拥有所有常用的新建、创建、显示、编辑、更新方法...

【问题讨论】:

  • 发布您的实际 routes.rb 文件
  • 用完整的路由文件更新了它。

标签: ruby-on-rails rest routing routes


【解决方案1】:

我认为您有两个问题之一:rake routes 的输出被您的终端屏幕破坏,或者您的路线被您安装的其他东西覆盖,例如 rails 引擎。

第一个很容易检查。听起来你有一个基本的用户脚手架设置(并没有太多其他设置)所以启动script/server,转到http://localhost:3000/users/new。如果您看到新用户页面,则说明您遇到了终端显示问题,但您的路线很好。但是,如果您看到用户索引页面,请继续下一步。

仔细检查您在上面发布的路线文件确实是您的应用程序的文件。这听起来很荒谬,但在某些编辑器中很容易打开错误的文件。例如,在 TextMate 中,如果你有一些 gems vendor 并通过 command-T 打开路由文件,你可以有多个 routes.rb 文件可供选择。

如果您确定查看的是正确的路由文件,下一步是检查您的应用程序是否有任何其他可能覆盖您的主文件的 routes.rb 文件。从终端,您可以运行find ./ -name routes.rb,这将列出任何其他路由文件。如果您安装了任何 Rails 引擎,这种情况尤其可能发生。

让我知道这是怎么回事 - 如果您仍然有问题,您可以压缩您的应用程序并将其通过电子邮件发送给我,我会看看。

【讨论】:

  • sooo 我知道是什么导致了这个问题。但是不知道为什么……原来是bundler!一旦我从 boot.rb 中取出捆绑器代码,路由就会自行纠正......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
  • 2015-06-23
  • 2011-12-21
  • 2015-02-19
相关资源
最近更新 更多