I’m a novice developer, some experience with rails but still have a lot to learn… I am developing a rails app that will have a form like the following:
select favorite colors:
[ ] red
[ ] blue
[ ] green
which will be stored in a table that looks like this:
ID
username
colors
So I am looking to store the array of colors into the colors column, and be able to go back and edit the record to adjust checkboxes. I’m not sure how to go about implementing this, thinking it would require some sort of form helper magic but then again maybe not.
Any guidance on how I could best achieve this would be appreciated.
This is pretty basic stuff. What you’re looking for is a
has_onerelationship between aUserandColorsmodel.Actually, if it’s checkboxes not radioboxes (user can select multiple colors), you need a
has_manyrelationship.See http://guides.rubyonrails.org/association_basics.html#the-has_one-association
and http://guides.rubyonrails.org/form_helpers.html
EDIT
A short and simple tutorial from Railscasts.com about a form helper gem, helped the idea of associations “click” in my mind. If you follow this 9 minute tutorial and think of substituting your
Colormodel for the sample veterinary application’sProblemmodel, you’ll be well on your way.http://railscasts.com/episodes/185-formtastic-part-2