title: Modify the user update function to require and validate the current password before allowing a user’s password to be changed
body: <p>The title I mentioned above is my assignment. When a user enters his current password it is showing error, password donot match. how to retrieve the current password to compare? 
  The code in my views/users/_form.html.erb is as folllows</p>
  
  <pre><code>     &lt;fieldset&gt;
        &lt;legend&gt;Enter User Details&lt;/legend&gt;
        &lt;div&gt;
        &lt;%= f.label :name %&gt;:
          &lt;%= f.text_field :name, :size =&gt; 40 %&gt;
          &lt;/div&gt;
            &lt;% if params[:action ]== "edit" || params[:action]== "update" %&gt;
      &lt;div&gt;
           &lt;%= f.label :current_password, :class =&gt; "long_label" %&gt;&lt;br /&gt;:
          &lt;%= f.password_field :current_password, :size =&gt; 40 %&gt;
           &lt;/div&gt;
      &lt;% end %&gt;
     &lt;div&gt;
       &lt;%= f.label :password, 'Password' %&gt;:
     &lt;%= f.password_field :password, :size =&gt; 40 %&gt;
     &lt;/div&gt;
      &lt;div&gt;
      &lt;%= f.label :password_confirmation, 'Confirm' %&gt;:
      &lt;%= f.password_field :password_confirmation, :size =&gt; 40 %&gt;
     &lt;/div&gt;
      &lt;div&gt;
       &lt;%= f.submit %&gt;
         &lt;/div&gt;
       &lt;/fieldset&gt;
  </code></pre>
  
  <p>And the code in my update controller is </p>
  
  <pre><code>        def update
          @user = User.find(params[:id])
        if @user.authenticate(params[:user][:current_password])
         cp = params[:user].delete('current_password')
      @user.errors.add(:current_password, 'is not correct') unless @user.authenticate(cp)
  respond_to do |format|
  if @user.errors.empty? and @user.update_attributes(params[:user])
  
      format.html { redirect_to user_url, :notice =&gt; 'Successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render :action =&gt; "edit" }
      format.json { render :json =&gt; @user.errors, :status =&gt; :unprocessable_entity }
    end
  end
  end
  </code></pre>
  
  <p>code in my usermodel is as follows </p>
  
  <pre><code>       class User &lt; ActiveRecord::Base
              attr_accessible :name, :password_digest, :password, :password_confirmation,    :current_password
         # attr_accessor :password validates :name, :presence =&gt; true, :uniqueness =&gt; true
  
            #validates :current_password, :presence =&gt; true
            validates :password, :presence =&gt;true
            validates_confirmation_of :password, :presence =&gt; true
             has_secure_password 
        after_destroy :ensure_an_admin_remains
        private
       def ensure_an_admin_remains
        if User.count.zero?
           raise "Can't delete last user"
       end
         end
  
      end
  </code></pre>
  
  <p>Is this the right way? How to write the condition to check with previous password? and it is asking template for errors. As far as I know, errors dont need templates..but here it is asking y I donno!!! 
  Any help is surely appreciated!!</p>