Rails 4,嵌套表单,ActiveModel :: ForbiddenAttributesError

时间:2015-06-23 08:50:43

标签: ruby-on-rails ruby strong-parameters

我需要为产品创建修订版,因此我必须在不同的表中移动可编辑的信息

我创建了嵌套表单,但遇到了保存数据的麻烦:ActiveModel :: ForbiddenAttributesError

据我了解,问题是因为命名,但我试着将其称为product_infos,product_infos_attributes等......没有任何工作

我还尝试通过更改

来改变行为
config.action_controller.action_on_unpermitted_parameters = false

但我仍然得到这个例外

如果我将.create更改为.new并将手动分配所有数据,它可以正常工作,但是有很多字段,我宁愿使用质量分配

有没有办法获得导致问题的字段名称?

代码:

控制器:

def create
  product = Product.new(product_params)

  if product.save params[:product].merge(:user_id => current_user.id)
    flash[:success] = t('msg.saved')
    redirect_to product_path(product)
  else
    render 'new'
  end
end

def product_params
  params[:product][:slug] = make_slug(params[:product][:product_info][:caption])
  params.require(:product).permit(:image, product_infos: [:caption, :description])
end

型号(产品):

class Product < ActiveRecord::Base
  has_many :product_infos
  accepts_nested_attributes_for :product_infos    

  def save(params)
    self.user_id = params[:user_id]
    self.slug = params[:slug]

    if super
      # I have this error here
      revision = self.product_infos.create(params[:product_info])

      params[:image].each do |file|
        self.product_images.create(:image => file)
      end

      true
    end
  end
end

model(product_info):

class ProductInfo < ActiveRecord::Base
  belongs_to :product
end

形式:

<%= form_for @product, :html => {:multipart => true} do |f| %>

    <%= f.fields_for :product_info do |i| %>
      <%= i.label :caption %>
      <%= i.text_field :caption, class: 'form-control', required: 'required' %>
    <% end %>
 <% end %>

错误:

    ActiveModel::ForbiddenAttributesError in ProductsController#create
ActiveModel::ForbiddenAttributesError

Extracted source (around line #57):
55
56
57
58
59
60


    if super
      revision = self.product_infos.new(params[:product_info])
      revision.caption = params[:product_info][:caption]
      revision.coordinates = params[:product_info][:coordinates]
      revision.user_id = params[:user_id]

Rails.root: /Users/lasoweq/Sites/ruby/eshop

Application Trace | Framework Trace | Full Trace
app/models/product.rb:57:in 'save'
app/controllers/products_controller.rb:22:in 'create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"Iwg5hdVB+5aAt+Mij8Tg7mMJv+lUr1RXzKciwRVC/ATmtU1FH3odLNaNDhssWz8vbUK+YSTvT0ErB+3cAI5/Tg==",
 "product"=>{"product_info"=>{"caption"=>"Test123",
 "coordinates"=>"13.727685539497559,
100.63547114393305",
 "description"=>"<p>adasdasdasd</p>",
 "sizes"=>"asdasdasd",
 "height"=>"asdasd",
 "admission"=>"zxczxcz",
 "free_admission"=>"0",
 "custom_info"=>"zxczxczxc"},
 "categories"=>["",
 "3"],
 "image"=>[#<ActionDispatch::Http::UploadedFile:0x007fd529922c18 @tempfile=#<Tempfile:/var/folders/yt/dtw20h393hvd4xfwcjzhcqxr0000gn/T/RackMultipart20150623-76404-c0nkwa.jpg>,
 @original_filename="GrizzlyBear1920x12005-23-2011_12_51_12_PM.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"product[image][]\"; filename=\"GrizzlyBear1920x12005-23-2011_12_51_12_PM.jpg\"\r\nContent-Type: image/jpeg\r\n">,
 #<ActionDispatch::Http::UploadedFile:0x007fd529922ab0 @tempfile=#<Tempfile:/var/folders/yt/dtw20h393hvd4xfwcjzhcqxr0000gn/T/RackMultipart20150623-76404-1si889i.jpg>,
 @original_filename="lion.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"product[image][]\"; filename=\"lion.jpg\"\r\nContent-Type: image/jpeg\r\n">]},
 "commit"=>"Сохранить"}

2 个答案:

答案 0 :(得分:0)

我假设您的关联和数据库模型已正确设置。 (product_info模型的belongs_to和产品模型的has_many)

您必须将accepts_nested_attributes_for :product_infos添加到产品型号中,以允许嵌套属性。控制器中的参数白名单可能是一个问题,但在您的情况下似乎已正确定义。

尝试将上面的行添加到模型中。

答案 1 :(得分:0)

producthas_manyproduct_infos的关系,所以此行

<%= f.fields_for :product_info do |i| %>

应该是

<%= f.fields_for :product_infos do |i| %>

而且您必须在:product_infos方法中将:prodcut_infos_attributes更改为product_params