Rails 4 + Devise Invitable:重设密码
Rails 4 + Devise Invitable: Reset Password
我有一个用户索引,我想添加一个按钮,当我点击它时会发送密码重置电子邮件(当用户失去邀请时)。
# 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 使用一个表单来重置密码,但我想我可以覆盖它,因为电子邮件是已知的并且可以在参数中提供
您可以这样做并使用 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
我有一个用户索引,我想添加一个按钮,当我点击它时会发送密码重置电子邮件(当用户失去邀请时)。
# 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 使用一个表单来重置密码,但我想我可以覆盖它,因为电子邮件是已知的并且可以在参数中提供
您可以这样做并使用 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