# Using Rails form helpers on non-existent methods

**URL:** <https://www.ruby-forum.com/t/using-rails-form-helpers-on-non-existent-methods/183744>\
**Category:** Rails\
**Created:** [February 19, 2010, 10:55am UTC](https://www.ruby-forum.com/t/using-rails-form-helpers-on-non-existent-methods/183744 "2010-02-19T10:55:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![niczm25](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@niczm25](https://www.ruby-forum.com/u/niczm25)\
**Post date:** [February 19, 2010, 10:55am UTC](https://www.ruby-forum.com/t/using-rails-form-helpers-on-non-existent-methods/183744/1 "2010-02-19T10:55:19Z")

</div>

Normally, when using form helpers in Rails, each field directly  
correlates to a method on the appropriate object.

However, I have a form (user signup) that needs to include fields that  
are not part of the user model itself (for instance, card details) but  
need to appear.

How can I build the form so that I can get the necessary fields, and  
validate them as I need to (so that it fits with all my other  
validations) without dirty-ing my user model?

Essentially I’m not building a form that directly correlates to the  
model in question. For instance a signup form might incorporate some  
user properties, and some “throw away” properties (such as those you  
might fire at a CC gateway and then forget).

If I created attributes on my user model, I would always need to  
supply those properties for validations to pass whereas I only want to  
validate these items on this single form in this single location.

What’s the approach to take here?

---

<div class="post-metadata">

**Author:** ![niczm25](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@niczm25](https://www.ruby-forum.com/u/niczm25)\
**Post date:** [February 19, 2010, 12:55pm UTC](https://www.ruby-forum.com/t/using-rails-form-helpers-on-non-existent-methods/183744/2 "2010-02-19T12:55:58Z")

</div>

You can use attr\_accessor to create new model attributes that are not  
present in the database.

For example, lets say you want to have a field “password” that the user  
types in, but this field is not going to be stored in the database.  
Instead the table will have a field “encrypted\_password” that is created  
from the “password” field that the user submits.

class User \< ActiveRecord::Base

# create a ‘virtual’ attribute

attr\_accessor :password

# validate its presence only when creating a new record

validates\_presence\_of :password, :on =\> :create

# set the database field “encrypted\_password” based on “password”

before\_create :encrypt\_password

private

def encrypt\_password  
#set encrypted PW with industrial strength encryption  
self.encrypted\_password = self.password.reverse  
end

end

---

<div class="post-metadata">

**Author:** ![niczm25](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@niczm25](https://www.ruby-forum.com/u/niczm25)\
**Post date:** [February 19, 2010, 3:12pm UTC](https://www.ruby-forum.com/t/using-rails-form-helpers-on-non-existent-methods/183744/3 "2010-02-19T15:12:42Z")

</div>

Neil M. wrote:

> Normally, when using form helpers in Rails, each field directly  
> correlates to a method on the appropriate object.
> 
> However, I have a form (user signup) that needs to include fields that  
> are not part of the user model itself (for instance, card details) but  
> need to appear.
> 
> How can I build the form so that I can get the necessary fields, and  
> validate them as I need to (so that it fits with all my other  
> validations) without dirty-ing my user model?
> 
> Essentially I’m not building a form that directly correlates to the  
> model in question. For instance a signup form might incorporate some  
> user properties, and some “throw away” properties (such as those you  
> might fire at a CC gateway and then forget).
> 
> If I created attributes on my user model, I would always need to  
> supply those properties for validations to pass whereas I only want to  
> validate these items on this single form in this single location.
> 
> What’s the approach to take here?

If you can’t use a virtual attribute as Sharagoz described, then you  
probably want to put the extra fields in a separate model and use  
accepts\_nested\_attributes\_for or something.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
