How to Keep Non-Object Form Data

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?

On 11/17/06, Jason N. [email protected] 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

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.

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.

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 :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs