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 [ ] greenwhich will be stored in a table that looks like this:
ID username colorsSo I am looking to store the array of colors into the colors column, and be able t开发者_StackOverflowo 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_one
relationship between a User
and Colors
model.
Actually, if it's checkboxes not radioboxes (user can select multiple colors), you need a has_many
relationship.
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 Color
model for the sample veterinary application's Problem
model, you'll be well on your way.
http://railscasts.com/episodes/185-formtastic-part-2
精彩评论