adding nested attributes with checkboxes in rails 4 using mass assignment

1.2k views Asked by At

error:

param is missing or the value is empty: color

I am making a form where I can add nested attributes to a parent record and I want to add them through checkboxes. I have the parent model "Car" and the child model "Colors"…but I want to start with some default "colors"…so I also have the model "Sample_Colors", which changes based on the "Car_Models".

I am trying to add multiple "Colors" to an associated "Car" using checkboxes…I do NOT want a HABTM relationship with "Sample_Colors" and "Colors", as I need the "Colors" record to be editable and not just a join table. I have done this using HABTM, and so I don't really understand why I can't create a non-join-table record in a similar way.

Where I'm having trouble is with the mass assignments...it either throws the error above or can't find the colors_id...

to clarify what I'm trying to do:

The checkboxes need to
1. Create a new "Color" Record (@color.new) that is associated with the @car record parent
2. Set the "value_one" column of the @color.new record to the sample_color.value_one value
3. Set the "value_two" column of the @color.new record to the sample_color.value_two value

4. The amount of checkboxes that are created == the @sample_colors that are iterated.

car_model.rb

class CarModel
has_many :sample_colors, dependent: :destroy
has_many :cars, dependent: :destroy

car.rb

class Car   
has_many :colors, dependent: :destroy
belongs_to :car_model
accepts_nested_attributes_for :colors, allow_destroy: true

sample_color.rb

class SampleColor
belongs_to :car_model

color.rb

class Color
belongs_to :car
accepts_nested_attributes_for :finishes, allow_destroy: true

_form (for adding colors)

    <%= form_for @car do |f| %>

    <%= f.fields_for 'car[color_attributes][]', @color, index: nil do |f| %>

<label class="form-label dk-aqua">Colors for <%= @car.car_name %></label><br>
<div class="row ">

 <%= hidden_field_tag "car[color_ids][]", nil %>
 <% @sample_colors.each do |sample_color| %>
   <%= check_box_tag "car[color_ids][]", "#{sample_color.id}"  %>  
   <%= hidden_field_tag "car[color_value_ones][]", "#{sample_color.value_one}" %> 
   <%= hidden_field_tag "car[color_value_twos][]", "#{sample_color.value_two}" %> 
   <%= label_tag :value_one, "#{sample_color.value_one}" %>  <br>

 <% end %>
</div>
 <% end %>

    <%= f.submit 'SAVE CHANGES',  :class => 'btn btn-green btn-lg btn-block' %>  
    <%end%>

cars_controller.rb

  def update
    @color = Color.new(color_params)
    @car.color_ids = params[:car][:color_ids] || []
    @car.color_value_ones = params[:car][:color_value_ones] || []
    @car.color_value_twos = params[:car][:color_value_twos] || []
        respond_to do |format|
      if @car.update(car_params)
        format.html { redirect_to @car, notice: 'Car was successfully updated.' }
        format.json { render :show, status: :ok, location: @car }
      else
        format.html { render :edit }
        format.json { render json: @car.errors, status: :unprocessable_entity }
      end
    end
  end

   def car_params
  params.require(:car).permit(:id, :car_name, :car_model_id, colors_attributes: [:id,  {:color_ids => [], :color_value_ones => [], :color_value_twos => []},  :value_one, :value_two,
  finishes_attributes: [:id, :value_one] ]  )
end

def color_params
  params.require(:color).permit(:id,  {:color_ids => [], :color_value_ones => [], :color_value_twos => []},  :value_one, :value_two,
  finishes_attributes: [:id, :value_one]  )
end
1

There are 1 answers

6
chad_ On

You could try replacing:

<% @sample_colors.each do |sample_color| %>
   <%= check_box_tag "car[color_ids][]", "#{sample_color.id}"  %>  
   <%= hidden_field_tag "car[color_value_ones][]", "#{sample_color.value_one}" %> 
   <%= hidden_field_tag "car[color_value_twos][]", "#{sample_color.value_two}" %> 
   <%= label_tag :value_one, "#{sample_color.value_one}" %>  <br>

 <% end %>

With collection_check_boxes:

<%= f.collection_check_boxes(:color_ids, @sample_colors, :id, :value_one) %>

I'm not entirely sure this is correct but it should be a step in the right direction. Let me know how it goes.