Rails 3 路由问题 [英] Rails 3 route problem
问题描述
在解决了另一个路由问题后,现在我又多了一个.
我的 routes.rb 中有这条路线:
<前><代码>匹配 "user/create_new_password/:reset_password_key" =>"users#create_new_password", :via=>[:post, :get], :as=>:create_new_password我可以像这样在我的功能测试中测试它:
<前><代码>测试应该创建新密码"做post :create_new_password, {:user=>{:password=>"123456", :password_confirmation=>"123456"}, :reset_password_key=>user.reset_password_key}结尾在我看来,我有以下形式:
<前><代码>=simple_form_for @user, :url=>create_new_password_path do |f|=f.input :password, :label=>I18n.t("activerecord.attributes.user.email")=f.input :password_confirmation, :label=>I18n.t("activerecord.attributes.user.password_confirmation")=f.submit I18n.t "activerecord.actions.user.create_new_password"当我提交表单时,我得到:
<前><代码>没有路由匹配/user/create_new_password/OqQxYTgjYKxXgvbAsTsWtMnIpMOpsjCRzLGZmJZLSbYtjvcvdpO"大字符串是 reset_password_key.
我已经在功能测试中使用相同的 reset_password_key 值对其进行了测试.
rake 路由的相关输出是:
<前><代码>create_new_password POST|GET/user/create_new_password/:reset_password_key(.:format) {:controller=>"users", :action=>"create_new_password"}我遗漏了一些东西...
作为对 BinaryMuse 评论的回答,我发现出了什么问题......我检查了 firebug 中的请求,发现 _method=put 正在发送邮政.Rails 聪明检测到我正在编辑用户 (@user) 的现有实例,因此它使用参数 _method 将 POTS 默认为 PUT.
问题是在我的路线中,我没有 :via 数组中的 PUT 方法.刚改成:
<前><代码>匹配 "user/create_new_password/:reset_password_key" =>"users#create_new_password",:via=>[:get, :put], :as=>:create_new_password在控制器中:
<前><代码>def create_new_password如果 request.put?#更改密码别的#渲染模板结尾结尾After solving the other problem with routes , now I have another one.
I have this route in my routes.rb:
match "user/create_new_password/:reset_password_key" =>"users#create_new_password", :via=>[:post, :get], :as=>:create_new_password
I can test it in my functional tests like this:
test "should create new password " do
post :create_new_password, {:user=>{:password=>"123456", :password_confirmation=>"123456"}, :reset_password_key=>user.reset_password_key}
end
In my view, I have the following form:
=simple_form_for @user, :url=>create_new_password_path do |f|
=f.input :password, :label=>I18n.t("activerecord.attributes.user.email")
=f.input :password_confirmation, :label=>I18n.t("activerecord.attributes.user.password_confirmation")
=f.submit I18n.t "activerecord.actions.user.create_new_password"
When I submit the form, I get:
No route matches "/user/create_new_password/OqQxYTgjYKxXgvbAsTsWtMnIpMOpsjCRzLGZmJZLSbYtjvcvdpO"
The big string, is the reset_password_key.
I have tested it in functional tests with the same value for reset_password_key.
The relevant output for rake routes is:
create_new_password POST|GET /user/create_new_password/:reset_password_key(.:format) {:controller=>"users", :action=>"create_new_password"}
I'm missing something...
As answered to BinaryMuse's comment, I've found what went wrong... I checked the request in firebug and found that a _method=put was being sent with the POST. Rails cleverness detected that I'm editing and existing instance of User (@user), so it defaults the POTS to a PUT, using the param _method.
The problem is that in my routes I haven't the method PUT in the :via array. Just changed to:
match "user/create_new_password/:reset_password_key" =>"users#create_new_password",:via=>[:get, :put], :as=>:create_new_password
And in the controller:
def create_new_password
if request.put?
#change password
else
#render template
end
end
这篇关于Rails 3 路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!