好吧,我在更改密码时对用户进行身份验证时出现问题,我收到了邮件和密码并运行了self.authenticate
,但它一直没有给我回复
我使用与登录时相同的格式。这是代码
user_controller.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
user.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 %>
我只是想弄清楚为什么它一直让我失去我的成功以及如何在改变密码方面取得成功。
答案 0 :(得分: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
有解决问题的方法,我必须直接指定params,这就是