无法在 Rails 4 中使用回形针保存图像属性

2024-05-18

我的 Rails 4 应用程序中有两个关联的模型:product.rb and image.rb。图像模型允许使用回形针 gem 附加文件。

Images belong_to一个产品,一个产品has_many Images.

我想使用该产品new创建产品时查看以创建并附加图像。每次我尝试时,与回形针图像关联的参数都不会保存,并且最终会丢失nil.

以下是型号:

产品.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"}

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

EDIT

更仔细地查看日志,我发现出现了“未经允许的参数:图像”错误。即使添加后也是如此images_attributes: [:image] to my product_params method.


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

In ProductsController#create,不要单独构建图像,而是让嵌套属性处理它,然后执行以下操作:

@product = Product.new(product_params)

并允许嵌套参数(我想出了image_attributes: [:image] from 这个问题 https://stackoverflow.com/questions/19370946/unpermitted-parameters-on-multiple-paperclip-file-uploads-rails4):

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

我看到插入products并插入images.

如果这不起作用,您可以发布您的ProductsController#new以及您的新产品形式?

编辑后进行编辑:

我的里面有 3 种不同的东西app/views/products/new.html.erb and ProductsController#new这可能会影响您的结果:

  1. In the form_for, 我有multipart: true如在回形针文档 https://github.com/thoughtbot/paperclip#quick-start:

    <%= form_for @product, :url => products_path, :html => { :multipart => true } do |f| %>
    
  2. Because Product has_many :images,以我的形式我有fields_for :images代替fields_for :image:

    <%= f.fields_for :images do |i| %>
      <%= i.file_field :image %>
    <% end %>
    
  3. This fields_for页面上不会显示任何内容,除非在ProductsController#new我建立一个形象;请问您Product.new有机会这样做吗?

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

你们之间的这些差异有什么特殊原因吗?如果您更改它以匹配我的代码,您的代码可以工作吗?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在 Rails 4 中使用回形针保存图像属性 的相关文章

随机推荐