Created
July 26, 2012 07:51
-
-
Save CodeAbstract/3180822 to your computer and use it in GitHub Desktop.
Rails 3 + Devise: Change password manually
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
devise_for :users ,:controllers => {:passwords => "passwords"} do | |
resources :passwords | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same here.