Skip to content

How To: Allow users to edit their password

rebagliatte edited this page Jun 30, 2011 · 54 revisions

We have two options to allow users to edit their password:

  • Use the registerable module, which will give you both sign up and edit user features;
  • Handle your own passwords controller to allow users editing their password. Example:
devise_for :users, :controllers => {:passwords => "passwords"}
class PasswordsController < ApplicationController
  before_filter :authenticate_user!

  def edit
    @user = current_user
  end

  def update
    @user = current_user

    if @user.update_with_password(params[:user])
      sign_in(@user, :bypass => true)
      redirect_to root_path, :notice => "Password updated!"
    else
      render :edit
    end
  end
end

It is also crucial to have attr_accessible: :password, :password_confirmation (in addition to others) in you model. Otherwise, password and confirmation validation will not take place.

I have: attr_accessible :email, :remember_me, :first_name, :last_name, :address_street, :address_city, :address_state, :address_zip, :address_country, :password, :password_confirmation

If you don't want to use update_with_password, which will require to enter the current password, see How To: Allow users to edit their account without providing a password.

Clone this wiki locally