1

我使用资产和子资产模型作为模型来保存图像。我有具有许多资产的产品模型,但在保存产品模型之前,在构建过程中上传图像失败。

产品型号

class Product < ActiveRecord::Base

  has_one :pic, :as => :assetable, :class_name => "Pics::Product", :dependent => :destroy  

资产模型

class Asset < ActiveRecord::Base
  belongs_to :assetable, :polymorphic => true

  attr_accessible :assetable_id, :assetable_type, :attachment, :type
end

图片::产品模型(子资产模型)

class Pics::Product < Asset
  has_attached_file :attachment, 
                    :styles => { :large => "500x500>", 
                                 :medium => "190>x", 
                                 :small => "115x115>" },
                    :default_url => 'missing-product-:style.png'
  attr_accessible :attachment
end

产品#创建

class ProductsController < ApplicationController
  before_filter :authenticate_user!

  def create
    @product = Product.new(params[:product])

    puts "=========="
    ap @product.pic

    @product.build_pic

    puts "=========="
    ap @product.pic

    if @product.save
      render json: @product
    else
      render json: {:error => "Error in creating a product!"}, :status => :unprocessable_entity
    end
  end

我在之前和之后打印了@product.pic @product.build_pic。看起来它在被调用之前正在做正确的事情@product.build_pic,然后在被调用后被删除。没有具体的错误,所以我真的不知道为什么会这样......

产品输出#Create

==========
#<Pics::Product:0x007fd9a3350b40> {
                         :id => nil,
                       :type => "Pics::Product",
               :assetable_id => nil,
             :assetable_type => "Product",
       :attachment_file_name => "25-years-of-storage.jpg",
    :attachment_content_type => "image/jpeg",
       :attachment_file_size => 753780,
      :attachment_updated_at => Sat, 22 Dec 2012 00:13:22 UTC +00:00,
                 :created_at => nil,
                 :updated_at => nil
}
===========
#<Pics::Product:0x007fd9a7184da8> {
                         :id => nil,
                       :type => "Pics::Product",
               :assetable_id => nil,
             :assetable_type => "Product",
       :attachment_file_name => nil,
    :attachment_content_type => nil,
       :attachment_file_size => nil,
      :attachment_updated_at => nil,
                 :created_at => nil,
                 :updated_at => nil
}


Started POST "/products" for 127.0.0.1 at 2012-12-21 16:13:22 -0800
Processing by ProductsController#create as */*
  Parameters: {"product"=>{"name"=>"asdfasdfa", "description"=>"asdfasdf", "child_id"=>"1", "pic_attributes"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x007fd9a61a4dc0 @original_filename="25-years-of-storage.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[pic_attributes][attachment]\"; filename=\"25-years-of-storage.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/RackMultipart20121221-29846-xhrk9k>>}, "category_id"=>"1", "target_age_min"=>"1", "target_age_max"=>"2", "purchase_date"=>"2012-12-02"}}
  User Load (1.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Command :: identify -format %wx%h '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]'
Command :: identify -format %m '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]'
Command :: identify -format %m '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]'
Command :: convert '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]' -auto-orient -resize "500x500>" '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk20121221-29846-a234mw'
Command :: file -b --mime '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk20121221-29846-a234mw'
Command :: identify -format %wx%h '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]'
Command :: identify -format %m '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]'
Command :: identify -format %m '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]'
Command :: convert '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]' -auto-orient -resize "190>" '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk20121221-29846-chaypg'
Command :: file -b --mime '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk20121221-29846-chaypg'
Command :: identify -format %wx%h '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]'
Command :: identify -format %m '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]'
Command :: identify -format %m '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]'
Command :: convert '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk.jpg[0]' -auto-orient -resize "115x115>" '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk20121221-29846-vecmvv'
Command :: file -b --mime '/var/folders/nm/gj73lnln3c57813fmlm_cxxr0000gn/T/25-years-of-storage20121221-29846-16vdudk20121221-29846-vecmvv'
   (0.2ms)  BEGIN
   (0.2ms)  COMMIT
   (0.2ms)  BEGIN
[paperclip] Scheduling attachments for deletion.
[paperclip] Deleting attachments.
   (0.2ms)  COMMIT
   (0.2ms)  BEGIN
  SQL (0.8ms)  INSERT INTO "products" ("category_id", "child_id", "created_at", "description", "name", "purchase_date", "target_age_max", "target_age_min", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"  [["category_id", 1], ["child_id", 1], ["created_at", Sat, 22 Dec 2012 00:13:25 UTC +00:00], ["description", "asdfasdf"], ["name", "asdfasdfa"], ["purchase_date", Sun, 02 Dec 2012 00:00:00 UTC +00:00], ["target_age_max", 2], ["target_age_min", 1], ["updated_at", Sat, 22 Dec 2012 00:13:25 UTC +00:00]]
  SQL (0.7ms)  INSERT INTO "assets" ("assetable_id", "assetable_type", "attachment_content_type", "attachment_file_name", "attachment_file_size", "attachment_updated_at", "created_at", "type", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"  [["assetable_id", 16], ["assetable_type", "Product"], ["attachment_content_type", nil], ["attachment_file_name", nil], ["attachment_file_size", nil], ["attachment_updated_at", nil], ["created_at", Sat, 22 Dec 2012 00:13:25 UTC +00:00], ["type", "Pics::Product"], ["updated_at", Sat, 22 Dec 2012 00:13:25 UTC +00:00]]
[paperclip] Saving attachments.
   (0.5ms)  COMMIT
Completed 200 OK in 3320ms (Views: 1.0ms | ActiveRecord: 9.9ms)
4

1 回答 1

0

嗯....我刚刚解决了我自己的问题。而不是调用 new 并构建接受的属性,调用 create 只是工作(@product = Product.create(params[:product]))。

问题是我不知道为什么!:(

于 2012-12-22T00:40:56.597 回答