error:
expected Array (got Rack::Utils::KeySpaceConstrainedParams) for param `car'
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.
To make the mass assignment work, I've been looking at this question: How to submit multiple NEW items via Rails 3.2 mass-assignment
The checkboxes need to
1. Create a new "Color" Record (@color.new) that is associated with the @car record parent
2. Set the "text" column of the @color.new record to the sample_color.text value
3. Set the "gloss" column of the @color.new record to the sample_color.gloss value
4. The amount of checkboxes that are created == the @sample_colors that are iterated.
I built a paired down sample app to show more clearly: http://ift.tt/1GuxVWx
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
sample_color.rb
class SampleColor
belongs_to :car_model
color.rb
class Color
belongs_to :car
_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[]", "#{sample_color.text}" %>
<%= hidden_field_tag "car[color_glosses][]", "#{sample_color.gloss}" %>
<%= label_tag "car[color_texts]", "#{sample_color.text}" %> <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_texts = params[:car][:color_texts] || []
@car.color_glosses = params[:car][:color_glosses] || []
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_texts => [], :color_glosses => []}, :text, :gloss] )
end
def color_params
params.require(:color).permit(:id, :text, :gloss, {:color_ids => [], :color_texts => [], :color_glosses => []}, )
end
Aucun commentaire:
Enregistrer un commentaire