Rails 中忘记密码
我正在尝试在 Rails 中实现忘记密码的解决方案。我有一个表格供用户输入其注册帐户的电子邮件地址,并且我打算让邮件程序通过电子邮件向他们发送一个唯一的 URL,将其链接到密码重置页面。
我的 config/routes.rb 文件具有以下路由:
resources :users do
collection do
get :lost_password #the account-email submisison form url
get :reset_password #a url for the function that sends the response email
end
end
当我从控制台运行 rake paths 时,我得到了我想要的路径:
lost_password_users GET /users/lost_password(.:format) {:action=>"lost_password", :controller=>"users"}
reset_password_users GET /users/reset_password(.:format) {:action=>"reset_password", :controller=>"users"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
但是!当用户点击下面代码中概述的表单上的提交按钮时:
<h3>Reset Password</h3>
<%= form_for(:user, :url => reset_password_users_path) do |f| %>
<p>Enter the email address you used to register for this site.</p></br>
<div class="field">
<%= f.label :email %> </br>
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit "Send Email" %>
</div>
<% end %>
我得到一个
没有路线匹配 “/用户/重置密码”
通过 ActionController 出现
错误。事实上,我确实为 lost_password_users_path 和 reset_password_users_path 定义了视图和控制器函数,所以我很困惑为什么会遇到这个路由错误。
我有两个问题:
- 当我明确定义了路径、方法和视图时,为什么 ActionController 会引发此错误?
- 是否有其他人在 RoR 中实施了密码重置,您能否提供有关此方法是否是良好实践的见解?
提前致谢!
I am trying to implement a forgotten password solution in rails. I have a form for the user to enter the email address for their registered account, and I intend to have a mailer email them a unique URL that will link them to a password reset page.
My config/routes.rb file has the following routes:
resources :users do
collection do
get :lost_password #the account-email submisison form url
get :reset_password #a url for the function that sends the response email
end
end
When I run rake routes from the console, I get the paths I want:
lost_password_users GET /users/lost_password(.:format) {:action=>"lost_password", :controller=>"users"}
reset_password_users GET /users/reset_password(.:format) {:action=>"reset_password", :controller=>"users"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
BUT! When the user hits the submit button on the form outlined in the code below:
<h3>Reset Password</h3>
<%= form_for(:user, :url => reset_password_users_path) do |f| %>
<p>Enter the email address you used to register for this site.</p></br>
<div class="field">
<%= f.label :email %> </br>
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit "Send Email" %>
</div>
<% end %>
I get a
No route matches
"/users/reset_password"
error through the ActionController.
I do, in fact, have views and controller functions defined for both the lost_password_users_path and the reset_password_users_path, so I'm puzzled as to why I would run into this routing error.
I have two questions:
- Why would the ActionController raise this error, when I clearly have the path, methods, and views defined?
- Has anyone else implemented a password reset in RoR, and can you lend any insight as to whether or not this approach is good practice?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将routes.rb中的
get :reset_password
更改为post :reset_password
Try changing
get :reset_password
topost :reset_password
in routes.rb重置密码功能:我希望它能起作用,在控制器更新功能中使用
function of reset password: I hope,it will be work, use in controller update function