Ruby on Rails
Tuesday, April 19, 2016
On Tuesday, April 19, 2016 at 4:09:21 PM UTC+1, Piotr Brych wrote:
We are working with nested attributes and we've reached a point where we're not sure if there is a better, clearer, "more rails" way of dealing with it.
We have 3 models - Organisation, Category and a strictly join table CategoryOrganisation (no additional attributes). The relation between Organisation and Category is many-to-many.
Now, we want to create a form with checkboxes through which the categories for organisations can be added and removed. The example form would look like that:
As long as you don't want the same form to be usable to create new categories, I wouldn't use nested attributes at all.
f.collection_check_boxes :category_ids, Category.all, :id, :name
will render a checkbox and label for each category, and the check box will be ticked if category_ids contains the element (there are plenty of options to customise the generated markup). I'm not entirely sure what happens if you assign to foo_ids but the overall save fails and rolls back. If you run into issues there I'd add a virtual attribute to hold the assigned ids and actually set category_ids from a before_save or similar.
Fred
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/756807e3-1bcf-451a-8b42-0689a6346174%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment