Closed colleeno closed 7 years ago
Hi Colleen, creating multiple models via the same controller is a bit tricky. What you can do is create checkboxes on the new
view for each category, then in the create
controller, iterate through the parameters and create an instance of your join model for each marked checkbox. For example:
A new
view for creating products with category associations:
https://github.com/AWhitleyGA/agora/blob/master/app/views/products/new.html.erb#L20-L25
Ignore the
user
parts of the form, they were specific to my domain
The associated create
action:
https://github.com/AWhitleyGA/agora/blob/master/app/controllers/products_controller.rb#L18-L32
Similar functionality on the edit
view:
https://github.com/AWhitleyGA/agora/blob/master/app/views/products/edit.html.erb#L20-L29
Associated update
action:
https://github.com/AWhitleyGA/agora/blob/master/app/controllers/products_controller.rb#L38-L52
A more complex but better practice for handling this is via nested forms. Give this section a read and see if you can adapt it to your domain:
http://guides.rubyonrails.org/form_helpers.html#building-complex-forms
ahhh thanks! ill check this all out!
I have a "categories" table, and "scripts" table (collection of tv/movie quotes), joined by tags.
Question 1 - I can only find examples (like our scribble exercise) to add tags(categories) after the item is created. Is there not a way to select when creating?
Question 2 - Either way, I've tested 3 ways to add tags on the "script" (quotes) edit page, having troubles setting them. Ideally I'd show preset tags, and a user could check multiples.
The checkbox works - this is my ideal, but it only creates 1 tag at a time. Is there a way to allow multiple selections to trigger multiple tags?
Text field works, but I don't want to use since I want to only allow preset tags. Skip this one.
Dropdown was my backup option to only allow certain tags, I'm displaying the options but not setting them. If I can't get multiples from a checkbox, curious how to use this to set.
form options and tag controller below, repo: repo: https://github.com/colleeno/project-scripts