无法使用Paperclip在Rails 4中保存图像属性

4
我在我的Rails 4应用程序中有两个关联模型:product.rbimage.rb。Image模型使用Paperclip gem附加文件。
图片belong_to一个产品,而一个产品has_many Images。
我想在创建产品时使用产品的new视图来创建和附加图像。每次尝试时,与Paperclip图像相关的参数都无法保存,并最终变成nil
以下是模型: Product.rb
class Product < ActiveRecord::Base
  validates :name, :part_number, presence: true
  has_many :images, dependent: :destroy
  belongs_to :category
  accepts_nested_attributes_for :images, allow_destroy: true
end

Image.rb

class Image < ActiveRecord::Base
  belongs_to :product
  has_attached_file :image
end

在过去的 Stack Overflow 问题中,我找到了一些相关的问题和答案,但似乎没有任何一个能帮助我的特殊情况。目前为止,我能够使用正确的属性提交文件,但它不会将它们保存到数据库中。

ProductsController#create

def create
  @product = Product.new(product_params)
end

def product_params
  params.require(:product).permit(:name,
                                  :category_id,
                                  :part_number,
                                  images_attributes: [:image])
end

ProductsController#new

def new
  @product = Product.new
  @categories = # irrelevant to question
end

products/new.html.haml

= form_for @product, :html => 
  = f.label :name
  = f.text_field :name
  = f.label :part_number
  = f.text_field :part_number
  = f.label :category_id
  = f.select :category_id, @ca
  = f.fields_for :image do |ff|
    = ff.label :image 
    = ff.file_field :image
  = f.submit('Create Product')

从参数看,我可以确定正确的属性已传递:

示例参数:

{"utf8"=>"✓",
 "authenticity_token"=>"jGNy/TasHzP0EcWDFzZ3XH5/fpxb6vD+ncQ2PZSQ3/I=",
 "product"=>{"name"=>"bar",
 "part_number"=>"foo",
 "category_id"=>"30",
 "image"=>{
 "image"=>#<ActionDispatch::Http::UploadedFile:0x007fc82f58e0e0
 @tempfile=#<Tempfile:/var/folders/04/23d9grkj5fv3wkj2t8858kx40000gn/T/RackMultipart20131029-64949-1ldkz4g>,
 @original_filename="FPE230_b.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"product[image][image]\"; filename=\"FPE230_b.jpg\"\r\nContent-Type: image/jpeg\r\n">}},
 "commit"=>"Create Product"}

然而,这张图片实际上并没有被保存到数据库中。我该怎么解决这个问题?

编辑

仔细查看日志后,我发现我收到了一个“未经许可的参数:image”错误。即使在我的product_params方法中添加了images_attributes:[:image]


请问您正在使用哪个版本的Paperclip? - carols10cents
最新版本:gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git" - michaelmichael
为了以后的参考,github.com/thoughtbot/paperclip 的最新提交是8e900b2a139deff9114efe84e6327235c92e6103。你能确认一下这是你正在使用的提交吗?它在两天前刚刚更新,并且经常更改。谢谢! - carols10cents
好的,我落后于HEAD一个或两个提交。在诊断问题时,为了稳定起见,我将使用3.5.2版本。 - michaelmichael
1个回答

3
我能够通过以下更改使设置与您的类似:
ProductsController#create 中,不要单独构建图像,让嵌套属性处理它,只需执行以下操作:
@product = Product.new(product_params)

并允许嵌套参数(我从这个问题中找到了image_attributes: [:image]):

def product_params
  params.require(:product).permit(:name, :part_number, images_attributes: [:image])
end

这是在一个create请求的日志中参数的样子:

{
   "utf8"=>"✓", 
   "authenticity_token"=>"7EsPTNXu127itgp4fbohu672/L4XnSwLEkrqUGec3pY=", 
   "product"=>{
     "name"=>"whatever", 
     "part_number"=>"12345", 
     "images_attributes"=>{
       "0"=>{
         "image"=>#<ActionDispatch::Http::UploadedFile:0x007feb0c183b08 
           @tempfile=#<Tempfile:/var/folders/6k/16k80dds0ddd6mhvs5zryh3r0000gn/T/RackMultipart20131031-51205-emaijs>, 
           @original_filename="some-image.png", 
           @content_type="image/png", 
           @headers="Content-Disposition: form-data; name=\"product[images_attributes][0][image]\"; filename=\"ponysay.png\"\r\nContent-Type: image/png\r\n">
        }
      }
    }, 
    "commit"=>"Create"}

我看到了向 productsimages 插入的操作。

如果这不起作用,您可以发布您的 ProductsController#new 和新产品表单吗?

在您的编辑之后:

我在我的 app/views/products/new.html.erbProductsController#new 中有三个不同的事情可能会影响您的结果:

  1. In the form_for, I have multipart: true as in the paperclip documentation:

    <%= form_for @product, :url => products_path, :html => { :multipart => true } do |f| %>
    
  2. Because Product has_many :images, in my form I have fields_for :images instead of fields_for :image:

    <%= f.fields_for :images do |i| %>
      <%= i.file_field :image %>
    <% end %>
    
  3. This fields_for doesn't show anything on the page unless in the ProductsController#new I build an image; does your Product.new do this by any chance?

    def new
      @product = Product.new
      @product.images.build
    end
    

您是否有特别的原因导致这些差异?如果您将代码更改以匹配我的代码,它能否正常运行?


д»Қ然дёҚиө·дҪңз”ЁгҖӮжҲ‘е·Із»Ҹж·»еҠ дәҶnewж–№жі•е’ҢиЎЁеҚ•пјҢеҶ’зқҖеӣ й—®йўҳиҖҢеҗ“и·‘жүҖжңүдәәзҡ„йЈҺйҷ©пјҢжҸҗдҫӣдәҶдёҖе өж–Үжң¬еўҷгҖӮ - michaelmichael
有时候需要一堵文字墙 :) - carols10cents
1
是在 fields_for 中的复数 images!谢谢! - michaelmichael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接