the_ the_ - 3 months ago 11
Ruby Question

Expected ProductField, got array issue

I have a rails 4 application that has a params block that looks like:

def store_params
params.require(:store).permit(:name, :description, :user_id, products_attributes: [:id, :type, { productFields: [:type, :content ] } ])

but I'm getting the error:

ActiveRecord::AssociationTypeMismatch in StoresController#create
ProductField expected, got Array

The parameters I'm trying to insert look like:

Parameters: {"utf8"=>"✓", "store"=>{"name"=>"fdsaf", "description"=>"sdfd","products_attributes"=>{"0"=>{"productFields"=>{"type"=>"", "content"=>""}}}}, "type"=>"Magazine", "commit"=>"Create store"}

My models are

  1. Store (has a
    has_many :products

  2. Product (has a
    has_many :productFields
    belongs_to :store

  3. ProductField (has a
    belongs_to :product

My view looks like:

<%= f.fields_for :products do |builder| %>
<%= render 'product_fields', f: builder %>
<% end %>

and then the product_fields partial:

<%= f.fields_for :productFields do |builder| %>
<%= builder.text_field :type%>
<%= builder.text_area :content %>
<% end %>


Make sure your Product and Store models have:


inside them.

Then, if your calling nested fields_for like that, make sure you build them (in the controller), something like:

product =