Rails 中忘记密码

发布于 2024-11-08 17:35:54 字数 1771 浏览 0 评论 0原文

我正在尝试在 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_pathreset_password_users_path 定义了视图和控制器函数,所以我很困惑为什么会遇到这个路由错误。

我有两个问题:

  1. 当我明确定义了路径、方法和视图时,为什么 ActionController 会引发此错误?
  2. 是否有其他人在 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:

  1. Why would the ActionController raise this error, when I clearly have the path, methods, and views defined?
  2. 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 技术交流群。

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

发布评论

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

评论(2

渡你暖光 2024-11-15 17:35:54

尝试将routes.rb中的get :reset_password更改为post :reset_password

Try changing get :reset_password to post :reset_password in routes.rb

好听的两个字的网名 2024-11-15 17:35:54

重置密码功能:我希望它能起作用,在控制器更新功能中使用

    if params[:user][:password].present?
    puts "present"
    puts params[:current_password]
    if (params[:user][:password] == "")
      params[:user].delete(:password) 
    else
      if @user.valid_password?(params[:current_password])
        @updated = true
        puts @updated.to_s
        @user.update_attributes(user_params)
        sign_in(@user,:bypass => true) 
        flash[:notice] = "Password Updated Successfully"
        redirect_back fallback_location: user_url
      else
        @updated = false
        puts @updated.to_s
        flash[:danger] = "Current Password does not matched"
        redirect_back fallback_location: user_url

      end
    end

function of reset password: I hope,it will be work, use in controller update function

    if params[:user][:password].present?
    puts "present"
    puts params[:current_password]
    if (params[:user][:password] == "")
      params[:user].delete(:password) 
    else
      if @user.valid_password?(params[:current_password])
        @updated = true
        puts @updated.to_s
        @user.update_attributes(user_params)
        sign_in(@user,:bypass => true) 
        flash[:notice] = "Password Updated Successfully"
        redirect_back fallback_location: user_url
      else
        @updated = false
        puts @updated.to_s
        flash[:danger] = "Current Password does not matched"
        redirect_back fallback_location: user_url

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