Using some information from rails_has_elegance and on the Internet, I came up with the following solution.
user / editing:
<%= form_for(@user, :html => {:multipart => true}) do |f| %> <%= render 'shared/error_messages', object: f.object %> <%= f.text_field :name, placeholder: :name %> <%= f.text_field :email, placeholder: :email %> <%= password_field_tag :current_password, params[:current_password], placeholder: "Current password" %> <%= f.password_field :password, placeholder: "New password (optional)" %> <%= f.password_field :password_confirmation, placeholder: "Confirm new password" %> <% end %>
User Model:
validates :password, :on => :create validates :password_confirmation, presence: true, :on => :update, :unless => lambda{ |user| user.password.blank? }
User controller:
def update @user = User.find(params[:id]) user = User.find_by_email(current_user.email).try(:authenticate, params[:current_password]) if user && @user.update_attributes(params[:user]) flash[:success] = "Profile updated" sign_in @user redirect_to @user else flash.now[:error] = "Incorrect Current Password" unless user sign_in @user render 'edit' end end
source share