我需要为新操作和编辑操作中的一个关联设置属性,所以我需要这样做:
产品型号
has_one :store
accepts_nested_attributes_for :store
形式
= form_tag @product do |f|
= f.fields_for :store do |store_fields|
= render 'store_form', :f => store_fields
在 Controller 中
params.require(:store).permit(:store).permit!
会显示字段,但是当我提交表单时,这没有任何意义,商店关联为空。如何解决问题?
UPD
params.require(:product).permit(store_attributes: [:store_id, :supplier_id, :margin, :discount]).permit!
日志:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "product"=>{"name"=>"qwefqwefasdf", "description"=>"", "permalink"=>"asdf", "store_attributes"=>{"margin"=>"123", "discount"=>"123"}}, "button"=>"", "id"=>"asdf"}
最佳答案
开启,正确的答案是
更改
= f.fields_for :store do |store_fields|
到 = f.fields_for :store, @vendor.store do |store_fields|
关于ruby-on-rails - has_one关联的嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20689478/