我有一个 Rails 4 应用程序,它有一个如下所示的 params 块:
def store_params
params.require(:store).permit(:name, :description, :user_id, products_attributes: [:id, :type, { productFields: [:type, :content ] } ])
end
但我收到错误:
ActiveRecord::AssociationTypeMismatch in StoresController#create
ProductField expected, got Array
我尝试插入的参数如下所示:
Parameters: {"utf8"=>"✓", "store"=>{"name"=>"fdsaf", "description"=>"sdfd","products_attributes"=>{"0"=>{"productFields"=>{"type"=>"", "content"=>""}}}}, "type"=>"Magazine", "commit"=>"Create store"}
我的模型是
- 商店(有一个
has_many :products
)
- 产品(有一个
has_many :productFields
and belongs_to :store
)
- ProductField(有一个
belongs_to :product
)
我的观点如下:
<%= f.fields_for :products do |builder| %>
<%= render 'product_fields', f: builder %>
<% end %>
然后是product_fields部分:
<%= f.fields_for :productFields do |builder| %>
<%= builder.text_field :type%>
<%= builder.text_area :content %>
<% end %>