【问题标题】:Polymorphic association and nested attributes多态关联和嵌套属性
【发布时间】:2013-08-22 13:24:39
【问题描述】:

我有一个产品模型和一个图像模型(基于回形针)。

class Product < ActiveRecord::Base
  has_many :images, as: :imageable,
                    dependent: :destroy
  accepts_nested_attributes_for :images
end

class Image < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
  has_attached_file :picture
end

我想在我创建产品的同一页面中为我的产品添加图片 (/products/new)。这是表格:

<%= form_for(@product, html: { multipart: true}) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div>
    <%= f.label :name, t('product.name') %>
    <%= f.text_field :name %>
  </div>                    
  <div class="file_field">
     <%= f.fields_for :image do |images_form| %>
       <%= images_form.file_field :image %>
     <% end %>
  </div>
  <%= f.submit @submit_button %>
<% end %>

然后我转到/products/new 页面,填写字段,然后单击提交按钮。服务器尝试渲染产品的展示页面,但由于图像尚未保存,因此无法正常工作。我有以下错误:

undefined method `picture' for nil:NilClass

对于产品展示页面中的以下行

image_tag(@product.images.first.picture.url(:medium))

我想知道为什么图像没有保存,我看到服务器呈现以下消息:

Unpermitted parameters: images_attributes

所以我在许可产品参数 (like there) 中添加了图像属性:

def product_params
  params.require(:product).permit(:name, :sharable, :givable, image_attributes: [:name, :picture_file_name, :picture_content_type, :picture_file_size])
end

但这并没有改变任何东西,我总是收到相同的错误消息。您知道权限问题来自哪里吗?

【问题讨论】:

  • params.require(:product).permit(:name, :sharable, :givable, image_attributes: [:name, :picture_file_name, :picture_content_type, :picture_file_size]) 到 params.require(:product).permit(:name, :sharable, :givable, :image)

标签: ruby-on-rails paperclip ruby-on-rails-4 polymorphic-associations


【解决方案1】:

你可能需要改变

params.require(:product).permit(:name, :sharable, :givable, image_attributes: [:name, :picture_file_name, :picture_content_type, :picture_file_size])

到

params.require(:product).permit(:name, :sharable, :givable, images_attributes: [:name, :picture])

【讨论】:

  • 干杯,乐于助人
  • 哦不!我弄错了。当我重新启动服务器时,产品的显示页面呈现良好,但图像不再保存。 #&lt;Image id: 3, name: nil, imageable_id: 249, imageable_type: "Product", picture_file_name: nil, picture_content_type: nil, picture_file_size: nil, picture_updated_at: nil, created_at: "2013-08-25 13:48:56", updated_at: "2013-08-25 13:48:56"&gt;]&gt;
  • 好的,您能否更新您问题中的信息以匹配当前状态(代码的最新版本、引发的错误等)
  • 好的,我找到了!我在允许的属性中用:picture 替换了:picture_file_name, :picture_content_type, :picture_file_size,现在它工作得很好。你想更新你的答案吗?
  • 感谢 Flo Rahl 的回答验证。团队合作 ;-)
【解决方案2】:

你定义了一个 has_many 关联,所以在你看来,它应该是 f.fields_for :images

<%= f.fields_for :images do |images_form| %>
    <div class="file_field">         
       <%= images_form.file_field :picture %>         
    </div>
<% end %>

在您的控制器中,您应该首先构建一些图像并将您的 images_attributes 添加到允许的参数中。

@product = Product.new
3.times {@product.images.build}

您的视图将显示 3 个文件字段。

【讨论】:

  • 不行,我老是出现“Unpermitted parameters: images_attributes”消息。
  • 您需要将 image_attributes 更改为 images_attributes
【解决方案3】:

这可能与您的file_field 身份有关

<%= images_form.file_field :image %>

而不是下面的?

<%= images_form.file_field :picture %>

【讨论】:

  • 不行,我老是出现“Unpermitted parameters: images_attributes”消息。
  • Flo Rahl,您能否将确切的错误消息添加到您的问题中?
  • 谢谢,我添加了一个新答案以保持清晰。告诉我它是否有帮助。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多