无法在 Rails 4 中使用 Paperclip 保存图像属性 [英] Can't Save Image Attributes with Paperclip in Rails 4

查看:46
本文介绍了无法在 Rails 4 中使用 Paperclip 保存图像属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Rails 4 应用中有两个关联的模型:product.rbimage.rb.Image 模型允许使用 Paperclip gem 附加文件.

I have two associated models in my Rails 4 app: product.rb and image.rb. The Image model allows attached files using the Paperclip gem.

图像 belong_to 一个产品,一个产品 has_many 图像.

Images belong_to a Product, and a product has_many Images.

我想在创建产品时使用产品的 new 视图来创建和附加图像.每次我尝试时,与 Paperclip 图像关联的参数都不会保存,并以 nil 结束.

I would like to use the Product's new view to create and attach an image when I create a product. Each time I try, the parameters associated with the Paperclip image do not get saved, and end up 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 问题,我发现了一些相关的问题和答案,但似乎没有一个对我的特定情况有帮助.到目前为止,我能够提交具有正确属性的文件,但它没有将它们保存到数据库中.

Looking through past Stack Overflow questions I've found some relevant questions and answers, but none that seem to help my particular situation. So far I'm able to submit the file with the correct attributes, but it doesn't save them to the database.

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')

查看参数我可以看出正在传递正确的属性:

Looking at the parameters I can tell that the correct attributes are being passed on:

示例参数:

{"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"}

但是,图像实际上并未保存到数据库中.我该如何纠正?

However, the image is not actually being saved to the database. How can I rectify this?

编辑

更仔细地查看日志,我发现我收到了不允许的参数:图像"错误.即使在将 images_attributes: [:image] 添加到我的 product_params 方法之后也是如此.

Looking more closely at the logs, I see that I'm getting an "unpermitted parameters: image" error. This is even after adding images_attributes: [:image] to my product_params method.

推荐答案

通过以下更改,我能够获得与您类似的设置:

I was able to get a setup similar to yours working with the following changes:

ProductsController#create中,不要单独构建图像,而是让嵌套的属性处理它,然后执行:

In ProductsController#create, instead of building the image separately, let the nested attributes handle it and just do:

@product = Product.new(product_params)

并允许嵌套参数(我从 这个问题):

And allow the nested parameters (I figured out the image_attributes: [:image] from this question):

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

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

This is what my parameters look like in the logs for a create request:

{
   "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"}

我看到一个插入到 products 和一个插入到 images.

And I see an insert into products and an insert into images.

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

If that doesn't work, could you post your ProductsController#new and your new product form?

编辑后

我的 app/views/products/new.html.erbProductsController#new 中有 3 种不同的东西可能会影响您的结果:

I have 3 different things in my app/views/products/new.html.erb and ProductsController#new that might be affecting your results:

  1. form_for 中,我有 multipart: true 就像 回形针文档:

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

  • 因为 Product has_many :images,在我的表单中我有 fields_for :images 而不是 fields_for :image:

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

  • 这个 fields_for 不会在页面上显示任何内容,除非在 ProductsController#new 中我构建了一个图像;您的 Product.new 是否有机会这样做?

  • 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
    

  • 您有这些差异有什么特别的原因吗?如果您更改它以匹配我的代码,您的代码是否有效?

    Are there any particular reasons for these differences you have? Does your code work if you change it to match mine?

    这篇关于无法在 Rails 4 中使用 Paperclip 保存图像属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆