1
votes

I have a user index and I want to add a button that would send a password reset email when I click it (for when users lose their invitation).

# 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

Normally Devise uses a form for password reset, but I figured I could override this since the email is known and can be supplied in the params

1

1 Answers

3
votes

You could do something like this and use Devise's existing methods, from documentation: 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