Rails 3 Manually Change Your Password

I am trying to invent my application. But I do not understand how I can give users the ability to change their password. I need a form with the fields "old password", "new password" and "new password confirmation". How should I do it?

If I use the default create form on the "/ profile" page

<%= render :template => 'devise/passwords/edit', 
                        :locals => { 
                          :resource => my_user_model_variable, 
                          :resource_name => my_user_model_name } %>

User.rb contains a string

attr_accessible :email, :password, :password_confirmation, :remember_me

But it was undefined method 'devise_error_messages!' for #<#<Class:0x59b9200>, and then (after commenting out the line devise_error_messages!) undefined method 'password' for #<Class:0x59b9200>.

I am trying to use my own PasswordsController:

class PasswordsController < ApplicationController
  before_filter :authenticate_user!

  def edit
    @user = current_user
  end

  def update
    @user = current_user
    raise params.inspect
    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

and use the tip on this: Rendering an edit password form

enter this code

<%= render :template => 'passwords/edit', 
                    :locals => { 
                      :resource => current_user, 
                      :resource_name => User } %>

to the "/ profile" page.

passwords / edit.html.erb contain this code

<h2>Change your password</h2>
<%# raise resource.inspect %>
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
  <%# devise_error_messages! %>
  <%= f.hidden_field :reset_password_token %>

  <p><%= f.label :password, "New password" %><br />
  <%= password_field_tag :name => "user[password]"%></p>
  <%= password_field_tag :name => "user[password_confirmation]"%></p>

  <p><%= f.submit "Change my password" %></p>
<% end %>

<%= render :partial => "devise/shared/links" %>

"/profile" action, .

+5
2

i.e password/edit.html.erb

<%# raise resource.inspect %>
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
  <%# devise_error_messages! %>
  <%= f.hidden_field :reset_password_token %>

   <p><%= f.label :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 %>

:

<h3>Change password </h3>
<hr/>
<%= render :template => 'passwords/edit', 
                    :locals => { :resource => current_user, :resource_name => "user" } %>

class PasswordsController < ApplicationController
  before_filter :authenticate_user!

  def edit
    @user = current_user
  end

  def update
    @user = current_user
    # raise params.inspect
    if @user.update_with_password(params[:user])
      sign_in(@user, :bypass => true)
      redirect_to root_path, :notice => "Your Password has been updated!"
    else
      render :edit,:locals => { :resource => @user, :resource_name => "user" }
    end
  end
end

devise_for :users ,:controllers => {:passwords => "passwords"} do
  end

  resources :passwords

;

+11

, , :

<%= render :template => 'passwords/edit', 
                    :locals => { 
                      :resource => current_user, 
                      :resource_name => "User" } %>
0

All Articles