Clear password fields if errors

I have a regular registration form with virtual_attributes:

attr_accessor :password_confirmation

def password
  @password
end

def password=(password)
  @password=self.crypted_password = User.encrypt(@password=pass, create_new_password_salt)
end

      

I would like to clear the password fields of a form when there are errors in the password. I figured out how to make the password field not show up on error using return, but I can't figure out how to return the password_confirmation field if there are errors in the password field.

the views are simple

<% form_for @user do |f| %>
  <%= f.password_field :password %>
  <%= f.password_field :password_confirmation %>
<% end %>

      

0


a source to share


1 answer


I'm not entirely clear on how your current password validation works, but how about something like this:

class User < ActiveRecord::Base
  ...
  validate :password_confirmation_matches

  def password_confirmation_matches
    if password != password_confirmation
      errors.add_to_base("You did not correctly confirm your password")
      self.password_confirmation = self.password = nil
    end
  end
end

      



Will this work for you?

+3


a source







All Articles