Paperclip不上传图像

时间:2014-04-04 02:06:35

标签: ruby-on-rails ruby file-upload paperclip

Ruby' 1.9.3'

Rails' 3.2.11'

Paperclip","〜> 4.1"

的ImageMagick-6.8.0-10

class Card < ActiveRecord::Base
  attr_accessible :logo_front
  attr_accessor :logo_front_file_name, :logo_front_content_type, :logo_front_file_size, :logo_front_updated_at

 has_attached_file :logo_front, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
 validates_attachment_content_type :logo_front, :content_type => /\Aimage\/.*\Z/
end


 Started POST "/cards" for 127.0.0.1 at 2014-04-03 19:44:10 -0600
 Processing by CardsController#create as HTML
 Parameters: {"utf8"=>"✓", "authenticity_token"=>"/e7XdW+Gne5WWQjuVjqZ4jMyBgvAQb5c/uBs46VKJ/4=",
"card"=>{"name"=>"test2", "category_id"=>"1", "card_number_length"=>"10",
"logo_thumbnail"=>"test2", "logo_front"=>#<ActionDispatch::Http::UploadedFile:0x007fa06d4741d0      @original_filename="test2.png",
 @content_type="image/png", @headers="Content-Disposition: form-data; name=\"card[logo_front]\";
 filename=\"test2.png\"\r\nContent-Type: image/png\r\n", 
 @tempfile=#<File:/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/RackMultipart20140403-49476- 1k3g1i5>>, "logo_back"=>"test2", "conditions"=>"test2"}, 
"commit"=>"Create Card"}



User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Command :: file -b --mime-type '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/fdcbe7cb00785a9f22e3850e31ee87ae20140403-49476-ldaz7u'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/a42d7b7bd0e5e0a24fe6bd1d68f19fb920140403-49476-klgp90[0]' 2>/dev/null
Command :: identify -format %m '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/a42d7b7bd0e5e0a24fe6bd1d68f19fb920140403-49476-klgp90[0]'
Command :: convert '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/a42d7b7bd0e5e0a24fe6bd1d68f19fb920140403-49476-klgp90[0]' -auto-orient -resize "300x300>" '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/a42d7b7bd0e5e0a24fe6bd1d68f19fb920140403-49476-klgp9020140403-49476-nno48v'
Command :: file -b --mime '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/a42d7b7bd0e5e0a24fe6bd1d68f19fb920140403-49476-klgp9020140403-49476-nno48v'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/a42d7b7bd0e5e0a24fe6bd1d68f19fb920140403-49476-klgp90[0]' 2>/dev/null
Command :: identify -format %m '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/a42d7b7bd0e5e0a24fe6bd1d68f19fb920140403-49476-klgp90[0]'
 Command :: convert '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/a42d7b7bd0e5e0a24fe6bd1d68f19fb920140403-49476-klgp90[0]' -auto-orient -resize "100x100>" '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/a42d7b7bd0e5e0a24fe6bd1d68f19fb920140403-49476-klgp9020140403-49476-1wtsixp'
Command :: file -b --mime '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/a42d7b7bd0e5e0a24fe6bd1d68f19fb920140403-49476-klgp9020140403-49476-1wtsixp'
(0.1ms)  begin transaction
Command :: file -b --mime-type '/var/folders/8d/lj5_232s33b2m6rf8tcwbgmm0000gn/T/265d61fad999f984d48a4e3dca5009d820140403-49476-1s5gzch'
SQL (13.3ms)  INSERT INTO "cards" ("card_number_length", "category_id", "code_type", "conditions", "created_at", "logo_back", "logo_front", "logo_thumbnail", "name", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["card_number_length", 10], ["category_id", 1], ["code_type", nil], ["conditions", "test2"], ["created_at", Fri, 04 Apr 2014 01:44:11 UTC +00:00], ["logo_back", "test2"], ["logo_front", nil], ["logo_thumbnail", "test2"], ["name", "test2"], ["updated_at", Fri, 04 Apr 2014 01:44:11 UTC +00:00]]

一切看起来都很好但是当SQL插入操作执行时,它会插入一个空值的记录[&#34; logo_front&#34;,nil]

我的目录路径有问题吗?

配置/环境/ development.rb

Paperclip.options[:command_path] = "/usr/local/bin/"

[UPDATE] 我刚刚意识到文件已成功保存在以下目录中:

public/system/cards/logo_fronts/000/000/005/medium/test/png

问题仍然是file_name未保存在DDBB

1 个答案:

答案 0 :(得分:1)

您的logo_front表格中似乎有cards字段。这是不正确的。

Card模型中,您正确地将logo_front定义为has_attached_file属性。

但是,您不应将logo_front直接添加到cards表。相反,Paperclip需要在cards表中包含4个特定字段:

logo_front_file_name
logo_front_file_size
logo_front_content_type
logo_front_updated_at

Paperclip提供了一个迁移生成器来完成这个任务:

rails generate paperclip card logo_front

注意:在撰写本文时,当前版本的Paperclip与Rails 4.0.4存在一个错误,即生成器不起作用(https://github.com/thoughtbot/paperclip/issues/1495)。如果遇到protected method 'migration_file_name'错误,您始终可以手动创建迁移:

class AddLogoFrontColumnsToCards < ActiveRecord::Migration
  def self.up
    add_attachment :cards, :logo_front
  end

  def self.down
    remove_attachment :cards, :logo_front
  end
end