0

好吧,我在更改他/她的密码时遇到了验证用户的问题,我得到了邮件和密码并运行了self.authenticate,但它一直返回我 nil

我使用与登录时相同的格式。这是代码

用户控制器.rb

def change_pw
    @user = User.authenticate(current_user.email, params[:password])
    logger.info current_user.email
    if @user.nil?

      logger.info params[:password]
      flash[:notice] = "incorrect password"
      redirect_to :back
    else
      if @user.valid?
        @user.password = @user.new_password unless @user.new_password.nil? || @user.new_password.empty?
        @user.save
        flash[:notice] = "Los cambios se han realizado exitosamente."
        redirect_to @user
      end  
    end   
end

用户.rb

def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
        return user
    else
        return nil
    end
end

形式

<%= form_for(@user, :url => "/change_password") do |f| %>

  <%= hidden_field(:user, :email, :value => @user.email) %>

  <div class="form-group">
    <div class="form-group col-md-4"><%= f.label :password %></div>
    <div class="form-group col-md-8"><%= f.password_field(:password, :class => "form-control") %></div>
  </div>

    <div class="form-group col-md-4"><%= f.label :new_password %></div>
    <div class="form-group col-md-8"><%= f.password_field(:new_password, :class => "form-control") %></div>

  <div class="form-group">
    <div class="form-group col-md-4"><%= f.label :new_password_confirmation %></div>
    <div class="form-group col-md-8"><%= f.password_field(:new_password_confirmation, :class => "form-control") %></div>    
  </div>

  <div class="col-md-offset-2 col-md-10">
    <button type="submit" class="btn btn-default">Change Password</button>
  </div>
<% end %>

我只是想弄清楚为什么它一直给我返回 nil 以及如何成功更改密码。

4

1 回答 1

0

我终于找到了我所有问题的原因

  def change_pw
    @user = User.authenticate(current_user.email, params[:user][:password])
    if @user.nil?
      flash[:notice] = "Contraseña incorrecta"
      redirect_to :back
    else
      if @user.valid?
        @user.password = params[:user][:new_password] unless params[:user][:new_password].nil? || params[:user][:new_password].empty?
        @user.save
        redirect_to @user
      end  
    end
  end

有办法解决它,我必须直接指定参数,仅此而已

于 2015-06-17T18:28:17.923 回答