Skip to content

Instantly share code, notes, and snippets.

@CodeAbstract
Created July 26, 2012 07:51
Show Gist options
  • Save CodeAbstract/3180822 to your computer and use it in GitHub Desktop.
Save CodeAbstract/3180822 to your computer and use it in GitHub Desktop.
Rails 3 + Devise: Change password manually
<%= form_for :user, :url => password_path do |f| %>
<p><%= f.label :current_password, "Current password" %><br />
<%= f.password_field :current_password %></p>
<p><%= f.label :password, "New password" %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Change my password" %></p>
<% end %>
class PasswordsController < ApplicationController
before_filter :authenticate_user!
def edit
@user = current_user
end
def update
@user = current_user
# Devise::Models::DatabaseAuthenticatable#update_with_password
# Update record attributes when :current_password matches, otherwise returns error on :current_password.
# It also automatically rejects :password and :password_confirmation if they are blank.
if @user.update_with_password(params[:user])
# Sign in the user bypassing validation in case his password changed
sign_in @user, :bypass => true
redirect_to root_path, :notice => "Your Password has been updated!"
else
flash[:alert] = @user.errors.full_messages.join("<br />")
render :edit
end
end
end
devise_for :users ,:controllers => {:passwords => "passwords"} do
resources :passwords
end
@bnymn
Copy link

bnymn commented Oct 3, 2015

Same here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment