预期的ProductField,出现数组问题 [英] Expected ProductField, got array issue
本文介绍了预期的ProductField,出现数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Rails 4应用程序,该应用程序具有如下所示的params块:
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 ] } ])
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
和belongs_to :store
) - ProductField(具有
belongs_to :product
)
- Store (has a
has_many :products
) - Product (has a
has_many :productFields
andbelongs_to :store
) - ProductField (has a
belongs_to :product
)
我的视图如下:
<%= f.fields_for :products do |builder| %>
<%= render 'product_fields', f: builder %>
<% end %>
,然后是product_fields部分:
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:
accepts_nested_attributes_for
在他们里面.
然后,如果您这样调用嵌套的fields_for,请确保您在控制器中构建它们,如下所示:
Then, if your calling nested fields_for like that, make sure you build them (in the controller), something like:
product = @store.products.build
product.productFields.build
这篇关于预期的ProductField,出现数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文