【问题标题】:Weird routing error in Rails 3.0Rails 3.0 中的奇怪路由错误
【发布时间】:2010-10-14 21:42:04
【问题描述】:

我一直在将我的 Rails 2.3.9 应用程序转换为 Rails 3.0,一切进展顺利。我用新系统创建了很多路线,所以我觉得我知道如何做到这一点。但是,我最近遇到了一个无法解决的路由错误。

我有一个这样定义的用户资源:

resources :users do
  member do
    get 'activate'
    get 'edit_password'
    post 'update_password'
  end

  collection do
    get 'forgot_password'
    post 'send_reset_instructions'
  end
end

失败的路由是 update_password。如果我将其更改为 get 方法,它就可以工作。但在用作帖子时不是,因为数据来自“edit_password”中的表单。

这是错误信息:

Routing Error

No route matches "/users/31/update_password"

这是 rake routes 的(相关)输出:

                activate_user GET    /users/:id/activate(.:format)                               {:action=>"activate", :controller=>"users"}
           edit_password_user GET    /users/:id/edit_password(.:format)                          {:action=>"edit_password", :controller=>"users"}
         update_password_user POST   /users/:id/update_password(.:format)                        {:action=>"update_password", :controller=>"users"}
        forgot_password_users GET    /users/forgot_password(.:format)                            {:action=>"forgot_password", :controller=>"users"}
send_reset_instructions_users POST   /users/send_reset_instructions(.:format)                    {:action=>"send_reset_instructions", :controller=>"users"}
                        users GET    /users(.:format)                                            {:action=>"index", :controller=>"users"}
                        users POST   /users(.:format)                                            {:action=>"create", :controller=>"users"}
                     new_user GET    /users/new(.:format)                                        {:action=>"new", :controller=>"users"}
                    edit_user GET    /users/:id/edit(.:format)                                   {:action=>"edit", :controller=>"users"}
                         user GET    /users/:id(.:format)                                        {:action=>"show", :controller=>"users"}
                         user PUT    /users/:id(.:format)                                        {:action=>"update", :controller=>"users"}
                         user DELETE /users/:id(.:format)                                        {:action=>"destroy", :controller=>"users"}

最后,这是该请求的 Web 服务器的输出 (webrick):

Started POST "/users/31/update_password" for 127.0.0.1 at 2010-10-14 23:30:59 +0200
  SQL (1.5ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  SQL (0.7ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'


ActionController::RoutingError (No route matches "/users/31/update_password"):


Rendered /home/jonatan/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.0ms)

你能找到其中的逻辑吗?

我正在使用通过 gem (3.0.1) 提供的最新 Rails。一开始我确实重写了 User-model 中的 *to_param* 来使用用户名,但无论如何问题都是一样的。

我检查了模型的 update_password 方法中的拼写错误,并确保它不是私有的。

【问题讨论】:

  • 如果将它从 update_password 更改为 foo(以及您的控制器方法以及视图名称)会发生什么?
  • 我尝试将其更改为“updatera_password”(几乎是瑞典语;)),但这也无济于事。我想也许 update_password 是某种 som 关键字,但显然情况并非如此。

标签: ruby-on-rails routing ruby-on-rails-3


【解决方案1】:

我也注意到了这一点,仅使用 POST 方法并且仅在使用“成员”时,与您的示例相同,即:

resources :forum_posts do
  member do
    post :preview
  end
end

会触发这个,但将其更改为 GET 或使用集合都可以正常工作,所以似乎是 rails 路由的错误;我现在通过在我的资源上方添加以下内容来解决这个问题:forum_posts do line:

match '/forum_posts/:id/preview(.:format)', :to => "forum_posts#preview", :as => :preview_forum_post

然后一切继续工作;简单的临时修复。

【讨论】:

  • 哇!谢谢!您的解决方法有效。奇怪的是,像这样应该很容易找到的 bug 竟然会溜走……
猜你喜欢
  • 2013-05-20
  • 2023-03-30
  • 1970-01-01
  • 2014-04-28
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多