0

I have this:

<%= f.collection_select :category_ids, Category.all, :id, :name, {} %>

inside my form and it creates the record no problem, however I'm lost on how to make more two or more records inside the same form, all the things I try either just create the one record or none at all. I found this solution, which I analyzed and was sure was gonna work (altough I don't really get on why it uses nil):

<%= f.fields_for :category_ids do |category| %>

  <%= category.collection_select(nil, Category.all, :id, :name,
    {include_blank: "---", selected: 0},
    {id: :event_category_id_1}) %>

  <%= category.collection_select(nil, Category.all, :id, :name,
    {include_blank: "---", selected: 0},
    {id: :event_category_id_2}) %>

<% end %>

but this time it creates no record at all.

Here are my models:

class Event < ApplicationRecord

  has_many :categorizations
  has_many :categories, through: :categorizations
  accepts_nested_attributes_for :categorizations

end

class Category < ApplicationRecord

  has_many :categorizations
  has_many :events, through: :categorizations

end

class Categorization < ApplicationRecord

  belongs_to :event
  belongs_to :category

end
Bluespheal
  • 123
  • 1
  • 10

1 Answers1

0

You want to assign more than one Category to your Model? Post your model so we can figure what's going wrong when storing it.

Adding multiple: true (and size: 5) will expand your select field and by holding ctrl/cmd you can select multiple entries.

<%= f.collection_select :category_ids, Category.all, :id, :name, {multiple: true, size: 5} %>
Thomas R. Koll
  • 3,131
  • 1
  • 19
  • 26
  • You can of course improve the UI, copy selected categories into a list below the select tag, stuff like that. – Thomas R. Koll May 12 '17 at 04:15
  • I've seen you can use the multiple: option, however I think it's a little hard for the user to select multiple, is there a way to just have two collection_select (or even a simple select) that create two different records in the same form? – Bluespheal May 12 '17 at 13:48
  • Ah, you have a `has_many through`, your fields_for need to be for `:categorizations`. Read at thoughtbot how to do those https://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through – Thomas R. Koll May 12 '17 at 18:18
  • sorry for the late response, but I'm still having problems with this, I've read thoughtbot multiple times and tried it different ways, but it still seems to noe be saving onto the database, here are the parameters that are sent: `categorizations_attributes:!ruby/hash:ActiveSupport::HashWithIndifferentAccess '0': !ruby/hash:ActiveSupport::HashWithIndifferentAccess category_ids: '3' '1': !ruby/hash:ActiveSupport::HashWithIndifferentAccess category_ids: '5'` – Bluespheal Jun 15 '17 at 18:05
  • nervermind, I solved ir by using: `<%= select_tag "categories_ids[]", options_from_collection_for_select(Category.all, "id", "name"), include_blank: true, :name => 'event[category_ids][]' %>` – Bluespheal Jun 15 '17 at 19:42