Rails 4 + Devise Invitable:重置密码

时间:2017-04-12 16:19:56

标签: ruby-on-rails ruby-on-rails-4 devise devise-invitable

我有一个用户索引,我想添加一个按钮,当我点击它时会发送密码重置电子邮件(当用户失去邀请时)。

# View
<% @users.each do |user| %>
    <%= link_to "Reset Password", reset_password_path(user) %> 
<% end %>

# Controller
def reset_password
    @user = User.find(params[:id])
    email = @user.email

    # Fire password reset...

    redirect_to users_path
end

通常Devise使用一个表单进行密码重置,但我想我可以覆盖它,因为电子邮件是已知的并且可以在参数中提供

1 个答案:

答案 0 :(得分:3)

您可以执行类似的操作并使用Devise现有的方法,来自文档:https://github.com/plataformatec/devise/wiki/How-To:-Mass-password-reset-and-email-notification

def reset_password
 #Generate random, long password that the user will never know:
 new_password = Devise.friendly_token(length = 50)

 @user = User.find(params[:id])
 @user.reset_password(new_password, new_password)

 #Send instructions so user can enter a new password:
 @user.send_reset_password_instructions

 redirect_to users_path
end