我一直在将我的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"
这是佣金路线的(相关)输出:
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方法中检查了拼写错误,并确保它不是私有的。
答案 0 :(得分:2)
我也注意到了这一点,只有使用POST方法并且只有在使用“member”时才这样,所以与你的例子相同,即:
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
然后一切都继续工作;容易临时修复。