سؤال

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

Everything looks fine but when the SQL Insert action executes, it inserts the record with a null value ["logo_front", nil]

Is it a problem with my directory path?

config/environments/development.rb

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

[UPDATE] I just realized that the files were successfully saved on the following directory:

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

The problem is still that the file_name was not saved in the DDBB

هل كانت مفيدة؟

المحلول

You seem to have the field logo_front in your cards table. This is not correct.

In your Card model you correctly defined logo_front as a has_attached_file attribute.

However, you should not add logo_front directly to the cards table. Instead Paperclip needs to have 4 specific fields in the cards table:

logo_front_file_name
logo_front_file_size
logo_front_content_type
logo_front_updated_at

Paperclip provides a migration generator to do exactly this:

rails generate paperclip card logo_front

Note: At the time of writing there is a bug in the current version of Paperclip with Rails 4.0.4 in that the generator does not work (https://github.com/thoughtbot/paperclip/issues/1495). If you hit a protected method 'migration_file_name' error, you can always create the migration manually:

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

  def self.down
    remove_attachment :cards, :logo_front
  end
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top