Rails 3 路线问题

发布于 2024-10-16 10:11:01 字数 1385 浏览 4 评论 0原文

解决了另一个路线问题后,现在我有了另一个问题。

我的routes.rb中有这条路线:


match "user/create_new_password/:reset_password_key" =>"users#create_new_password", :via=>[:post, :get], :as=>:create_new_password

我可以在我的功能测试中测试它,如下所示:


test "should create new password " do
    post :create_new_password, {:user=>{:password=>"123456", :password_confirmation=>"123456"}, :reset_password_key=>user.reset_password_key}
end

在我看来,我有以下表单:


=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"


当我提交表单时,我得到:


No route matches "/user/create_new_password/OqQxYTgjYKxXgvbAsTsWtMnIpMOpsjCRzLGZmJZLSbYtjvcvdpO"

大字符串,是reset_password_key。

我已经在功能测试中使用与reset_password_key相同的值对其进行了测试。

耙子路线的相关输出是:


create_new_password POST|GET /user/create_new_password/:reset_password_key(.:format) {:controller=>"users", :action=>"create_new_password"}

我错过了一些东西......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-10-23 10:11:01

正如对 BinaryMuse 的评论的回答,我发现出了问题......我检查了 firebug 中的请求,发现 _method=put 正在与 POST 一起发送。 Rails 聪明地检测到我正在编辑用户 (@user) 的现有实例,因此它使用参数 _method 将 POTS 默认为 PUT。

问题是在我的路线中,我没有在 :via 数组中使用 PUT 方法。只是更改为:


 match "user/create_new_password/:reset_password_key" =>"users#create_new_password",:via=>[:get, :put], :as=>:create_new_password

在控制器中:


def create_new_password
   if request.put?
      #change password
   else
     #render template
   end

end

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文