Rails 3.0 中奇怪的路由错误 [英] Weird routing error in Rails 3.0

查看:46
本文介绍了Rails 3.0 中奇怪的路由错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been converting my Rails 2.3.9 application to Rails 3.0 and everything has been going well. I've created a lot of routes with the new system so I feel like I know how to do this. However, I recently got a routing error I can't solve.

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

I have a users resource defined like this:

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"中的表单.

The failing route is update_password. If I change it to a get method it works. But not when used as a post, with data coming from a form in "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"}

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

Finally, this is the output of the web server for that request (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)

你能从中找到什么逻辑吗?

Can you find any logic in this?

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

I'm using the latest Rails available through gem (3.0.1). At first I did override *to_param* in the User-model to use the username, but the problem is the same no matter what.

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

I've checked for misspellings in the update_password method of the model, and also made sure it is not private.

推荐答案

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

I have noticed this as well, only with the POST method and only when using "member", so same as your example, ie:

resources :forum_posts do
  member do
    post :preview
  end
end

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

will trigger this, but changing it to GET or using collection both work fine, so seems to be a bug with rails routes; I get around this for now by adding the following just above my resources :forum_posts do line:

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

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

then everything continues working; easy temporary fix.

这篇关于Rails 3.0 中奇怪的路由错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆