【问题标题】:Rails 4, nested forms, ActiveModel::ForbiddenAttributesErrorRails 4,嵌套表单,ActiveModel::ForbiddenAttributesError
【发布时间】:2015-06-23 08:50:42
【问题描述】:

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

我创建了嵌套表单,但保存数据时遇到问题: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

型号(产品信息):

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"=>"Сохранить"}

【问题讨论】:

  • 请用关联模型代码更新您的帖子
  • 对不起,忘了这个
  • 尝试将&lt;%= f.fields_for :product_info do |i| %&gt;更改为&lt;%= f.fields_for :product_infos do |i| %&gt;
  • 您得到的确切错误是什么?
  • @Pavan 不错!很可能是这样,您应该将其添加为答案:)

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


【解决方案1】:

我假设您的关联和数据库模型设置正确。 (belongs_to 用于 product_info 模型,has_many 用于 product 模型)

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

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

【讨论】:

  • 对不起,我忘了这个,我已经更新了我的代码,我的模型中已经有这行了
  • 可能值得研究一下 Ryan Bates 的nested_form gem。这真的很容易。虽然它适用于 Rails 4,但它有点旧。
【解决方案2】:

你有一个producthas_many 关系到product_infos,所以这条线

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

应该是

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

您还必须在您的 product_params 方法中将 :product_infos 更改为 :prodcut_infos_attributes

【讨论】:

  • 如果我将使用product_infos,则不会显示字段。然后所有块 fields_for 都丢失了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多