views:

109

answers:

1

I have a regular signup 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 the form when there are errors on password. I figured out how to make the password field not show on error using return, but I can't figure out how to make the password_confirmation field return if there are errors on the password field.

the views are just simple

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

It's not entirely clear to me 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

Would that work for you?

molf
Might try it! Thanks
Cameron
I modified this a bit but it worked nicely. Thanks
Cameron