【问题标题】:Forgotten Password in Rails在 Rails 中忘记密码
【发布时间】:2011-08-28 17:45:22
【问题描述】:

我正在尝试在 Rails 中实现忘记密码的解决方案。我有一个表格供用户输入其注册帐户的电子邮件地址,并且我打算让邮寄者通过电子邮件向他们发送一个唯一的 URL,该 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 routes 时,我得到了我想要的路径:

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

我得到一个

没有路线匹配 "/users/reset_password"

通过 ActionController 出错。

事实上,我确实为 lost_password_users_pathreset_password_users_path 定义了视图和控制器函数,所以我很困惑为什么会遇到这种情况路由错误。

我有两个问题:

  1. 当我明确定义了路径、方法和视图时,为什么 ActionController 会引发此错误?
  2. 是否有其他人在 RoR 中实施了密码重置,您能否就这种方法是否是好的做法提供任何见解?

提前致谢!

【问题讨论】:

    标签: ruby-on-rails-3 rails-routing forgot-password


    【解决方案1】:

    重置密码功能:希望可以,在控制器更新功能中使用

        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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 谢谢,普拉文!仍在努力理解这一切是如何联系在一起的。 =P
      猜你喜欢
      • 2014-08-05
      • 2018-04-02
      • 1970-01-01
      • 2012-07-25
      • 2015-10-29
      • 2015-05-17
      • 2017-04-07
      • 1970-01-01
      • 2012-02-09
      相关资源
      最近更新 更多