Sean Magyar
Sean Magyar

Reputation: 2380

rails4 collection select with has_many through association and nested model forms

I have a rails4 app. At the moment my collection select only works if I select only one option. Below is my working code. I only have product form. Industry model is populated with seeds.rb. IndustryProduct is only use to connect the other 2 models.

I'd like to know what I have to change in the code to be able to choose more.

I saw some working examples with multiple: true option like (https://www.youtube.com/watch?v=ZNrNGTe2Zqk at 10:20) but in this case the UI is kinda ugly + couldn't pull it off with any of the sample codes. Is there an other solution like having more boxes with one option chosen instead of one box with multiple options?

models:

class Product < ActiveRecord::Base
  belongs_to :user
  has_many :industry_products
  has_many :industries, through: :industry_products
  has_many :product_features

  accepts_nested_attributes_for :industry_products, allow_destroy: true
  accepts_nested_attributes_for :product_features

  validates_associated :industry_products
  validates_associated :product_features
end

class Industry < ActiveRecord::Base
  has_many :industry_products
  has_many :products, through: :industry_products

  accepts_nested_attributes_for :industry_products
end

class IndustryProduct < ActiveRecord::Base
  belongs_to :product
  belongs_to :industry
end

_form.html.erb

<%= form_for @product do |f| %>
  <%= render 'layouts/error_messages', object: f.object %>
  ......
  <%= f.fields_for :industry_products do |p| %>
    <%= p.collection_select :industry_id, Industry.all, :id, :name %>
  <% end %>
  <%= f.fields_for :product_features do |p| %>
    <%= p.text_field :feature, placeholder: "add a feature", class: "form-control" %>
  <% end %>
  <%= f.submit class: "btn btn-primary" %>
<% end %>

products controller

def new
  @product = Product.new
  @product.industry_products.build
  @product.product_features.build
end

def create
  @product = current_user.products.new(product_params)
  if @product.save
    redirect_to @product
  else
    render action: :new
  end
end
......
def product_params
  params.require(:product).permit(....., industry_products_attributes: [:id, :industry_id, :_destroy], industries_attributes: [:id, :name], product_features_attributes: [:feature])
end

Upvotes: 3

Views: 5409

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

Firstly, you could fix your first collection select by using it to set the industry_ids for the @product:

<%= form_for @product do |f| %>
  <%= f.collection_select :industry_ids, Industry.all, :id, :name %>
<% end %>

This will allow you to set the collection_singular_ids method, which exists for all has_many associations.

You'd have to back it up in the params method:

#app/controllers/products_controller.rb
....
def product_params
  params.require(:product).permit(.... industry_ids: [])
end

A lot more succinct than using nested attributes.


To get that "multiple" selection, you'll want to use the following:

<%= f.collection_select :industry_ids, Industry.all, :id, :name, {}, { multiple: true } %>

Tested & working enter image description here

--

You may also want to look at collection_check_boxes:

<%= f.collection_check_boxes :industry_ids, Industry.all, :id, :name %>

Upvotes: 5

Related Questions