简体   繁体   English

多态关联和嵌套属性

[英]Polymorphic association and nested attributes

I have a Product model and an Image model (based on paperclip). 我有一个产品模型和一个图像模型(基于回形针)。

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

I want to add image for my products in the same page I create product ( /products/new ). 我想在我创建产品的同一页面( /products/new )中为产品添加图像。 Here is the form : 表格如下:

<%= 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 %>

Then I go to the /products/new page, I fill in the fields, and click on the submit button. 然后,我进入/products/new页面,填写字段,然后单击提交按钮。 The server try to renders the product's show page, but it doesn't work because the image has not been saved. 服务器尝试呈现产品的显示页面,但由于尚未保存图像,因此无法正常工作。 I have the following error : 我有以下错误:

undefined method `picture' for nil:NilClass

for the following line in the product's show page 用于产品显示页面中的以下行

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

I wonder why the image has not be saved and I see that the server renders the following message : 我不知道为什么尚未保存图像,并且看到服务器呈现以下消息:

Unpermitted parameters: images_attributes

So I added image attributes in the permit products params ( 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

But it doesn't change anything, I always have the same error message. 但这并没有改变任何东西,我总是有相同的错误消息。 Do you have an idea where does the permission issue come from ? 您是否知道权限问题来自何处?

You may need to change 您可能需要更改

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

to

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

You define a has_many association, so in your view, it should be f.fields_for :images 您定义了一个has_many关联,因此在您看来,它应为f.fields_for:images

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

In your controller, you should build some images first and add your images_attributes to permitted parameters. 在控制器中,您应该首先构建一些图像,然后将images_attributes添加到允许的参数。

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

your view will show 3 file fields. 您的视图将显示3个文件字段。

Any chance this has to do with your file_field being 任何可能与您的file_field

<%= images_form.file_field :image %>

instead of the following? 而不是以下内容?

<%= images_form.file_field :picture %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM