# How to Keep Non-Object Form Data

**URL:** <https://www.ruby-forum.com/t/how-to-keep-non-object-form-data/76355>\
**Category:** Rails\
**Created:** [November 17, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/how-to-keep-non-object-form-data/76355 "2006-11-17T20:23:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [November 17, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/how-to-keep-non-object-form-data/76355/1 "2006-11-17T20:23:16Z")

</div>

I have a registration form that submits information outside of the  
object. Specifically, they’re checkboxes asking if the user wishes to be  
contacted. They’re not related to an attribute of the User object, and  
if there are errors, the form is reloaded with all of the user  
information, but the check boxes blanked out. Is there a ‘Rails Magic’  
way to keep the values or do I have to hack it up?

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [November 17, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/how-to-keep-non-object-form-data/76355/2 "2006-11-17T20:35:19Z")

</div>

On 11/17/06, Jason N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a registration form that submits information outside of the  
> object. Specifically, they’re checkboxes asking if the user wishes to be  
> contacted. They’re not related to an attribute of the User object, and  
> if there are errors, the form is reloaded with all of the user  
> information, but the check boxes blanked out. Is there a ‘Rails Magic’  
> way to keep the values or do I have to hack it up?
> 
> >

Jason,

It seems to me an ok\_to\_contact field for the User model would be  
useful to keep around and persist to the database. Why would you not  
make this an attribute of the User model?

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [November 17, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/how-to-keep-non-object-form-data/76355/3 "2006-11-17T20:49:20Z")

</div>

We actually have a ‘subscription’ model which tracks which mailing lists  
the users want to be a part of. This form will just email our  
salespeople if the boxes are checked. We don’t need to track it. We  
actually fixed it by putting  
:checked =\> (params[:contact][:this\_item] != 0)? ‘checked’ : false  
On each checkbox, but that seems icky.

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [November 17, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/how-to-keep-non-object-form-data/76355/4 "2006-11-17T20:51:20Z")

</div>

Jason N. wrote:

> I have a registration form that submits information outside of the  
> object. Specifically, they’re checkboxes asking if the user wishes to be  
> contacted. They’re not related to an attribute of the User object, and  
> if there are errors, the form is reloaded with all of the user  
> information, but the check boxes blanked out. Is there a ‘Rails Magic’  
> way to keep the values or do I have to hack it up?

The most basic method is to recycle form data using the params  
hash directly:

check\_box\_tag :contact\_ok, some\_value, !params[:contact\_ok].nil?

If you want the box initially ticked you can set params[:contact\_ok]  
to some value in your controller code for the page GET.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [November 17, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/how-to-keep-non-object-form-data/76355/5 "2006-11-17T22:41:34Z")

</div>

You could have the magic if you wanted to

In your user class, simply make use of attribute accessors.

attr\_accessor :contact\_for\_promotion, :contact\_for\_something\_else

Then you can do

\<%=check\_box “user”, “contact\_for\_promotion” %\>

And the magic should work just fine.

You’ll even be able to do things with them in the model then.

HOWEVER… your situation seems like they should actually be database  
colums… that way

1. They persist and can be changed later so users can opt in / out
2. You simply need to do  
user.find\_all\_by\_contact\_for\_promotion.collect{|u| u.email}) to  
get an  
array of email addresses to send messages to 🙂
