嵌套fields_for Rails 4

时间:2015-07-01 19:07:10

标签: ruby-on-rails ruby ruby-on-rails-4

我有关于ral 4的双嵌套fields_for的问题许可。示例关系:

  Service - > has_many :product_services
  accepts_nested_attributes_for :product_services, allow_destroy: true
  Product Service -> has_many :foto_product_services
  accepts_nested_attributes_for :foto_product_services, allow_destroy: true

服务 - > new.html.slim

= form_for @service, :html => {:multipart => true} do |f|
  = f.fields_for :product_services do |builder|
    = render 'field_product', f: builder
= f.submit

服务 - > _field_product.html.slim

= f.text_field :price, :class => 'text_field input-lg width-100'
= f.fields_for :foto_product_services do |builder|
    = builder.file_field "avatar[]", type: :file, multiple: true

在我的服务控制器中允许Params

def service_params
  params.require(:service).permit(:service_category_id, :title, :description, :product_services_attributes => [:title, "_destroy"], :foto_product_services_attributes => [:avatar])
end

当我选择foto产品后单击按钮提交时,我得到错误未经许可的参数:foto_product_services_attributes。

1 个答案:

答案 0 :(得分:1)

您有深层嵌套属性,因此foto_product_services_attributes应位于product_services_attributes内:

params.require(:service).permit(:service_category_id, :title, :description, :product_services_attributes => [:title, "_destroy", :foto_product_services_attributes => [:avatar]])