[Rails] Extra Fields?

Jeremy Kemper jeremy at bitsweat.net
Thu Nov 11 21:02:50 GMT 2004


If you prefer to keep this behavior in your model, just define an 
attribute in your Model class.  It won't be persisted since it doesn't 
exist in the database, but all the Rails goodies will still play nice.

class Model < ActiveRecord::Base
   attr_accessor :password, :password_confirmation

   def validate
     errors.add('password', 'Password and confirmation do not match') 
unless password_confirmation == password
   end

   def before_save
     self.password_sha1 = Digest::SHA1(password) if password
   end
end

Best,
jeremy


Russ Smith wrote:
> I knew it had to be something simple like that. :-)
> Thanks
> 
> 
> On Nov 11, 2004, at 12:40 PM, Tobias Luetke wrote:
> 
>> I added extra fields called password_confirmation and
>> email_confirmation to my signup form and added this to the controller
>> :
>>
>>     @newuser.errors.add("password", "does not match")   unless
>> @newuser.password == @params['password_confirmation']
>>     @newuser.errors.add("email", "does not match")      unless
>> @newuser.email == @params['email_confirmation']
>>
>>
>>
>> On Thu, 11 Nov 2004 12:35:34 -0800, Russ Smith <russ at igeanetwork.com> 
>> wrote:
>>
>>> I'm wondering how you would go about putting a "confirm password" field
>>> into a form and not have it inserted into the database. An example
>>> would be on the Basecamp sign up form. And also on that page is an
>>> "accept terms" checkbox. I guess I want to know how do you run these
>>> fields through validation (model or controller) and not have them
>>> automatically entered into the database.


More information about the Rails mailing list