bobloblaw
bobloblaw

Reputation: 11

can't get local file to save into database using Rails 3 and paperclip

I have an Image model that has an attached file :photo and am attempting to save a local file into photo using the console.

My Image model:

class Image < ActiveRecord::Base
 has_attached_file :photo
 attr_accessor :photo_file_name
 belongs_to :item
end

My Image migrations:

class CreateImages < ActiveRecord::Migration
  def self.up
   create_table :images do |t|
   t.string :photo_file_name
   t.string :photo_content_type
   t.integer :photo_file_size
   t.datetime :photo_updated_at
   t.timestamps
 end
end

def self.down
 drop_table :images
end
end

At the console:

a=Image.new
 => #<Image id: nil, file_name: nil, content_type: nil, file_size: nil, created_at: nil, updated_at: nil, item_id: nil> 

a.photo=File.new('/home/bobloblaw/Pictures/nomnom.jpg')
 => #<File:/home/bobloblaw/Pictures/nomnom.jpg> 

a.save
 => true 

a.photo
 => /system/photos/2/original/nomnom.jpg?1302065614 

Image.first.photo
 => /photos/original/missing.png 

So, as you can see, a.photo contains the correct information, but somehow is not correctly saving it into Image in the actual database. I have also tried various combinations of adding things such as :photo and :photo_file_name to attr_accessible.

Any help would be appreciated as I've searched all over at haven't found anyone else with this problem!

Thanks

Upvotes: 1

Views: 1005

Answers (5)

nitecoder
nitecoder

Reputation: 5486

Give File.read, not File.new a try.

Upvotes: 0

henrik
henrik

Reputation: 133

Just replace

attr_accessor :photo_file_name

to

attr_accessor :photo

Upvotes: 1

acw
acw

Reputation: 1093

Try removing from your model definition. This accessor will be created automatically.

attr_accessor :photo_file_name

Upvotes: 0

Ajay Singh
Ajay Singh

Reputation: 1621

you have to give the :url and :path..

like this

has_attached_file :accountlogo,

                :storage => :s3,
                :s3_credentials => "#{RAILS_ROOT}/config/amazon_s3.yml",
                :url  => "/accountslogo/:id/:basename.:extension",
                :path => "/accountslogo/:id/:basename.:extension"
                # :path => ":rails_root/public/accountslogo/:id/:basename.:extension"

Upvotes: 0

Ryan Bigg
Ryan Bigg

Reputation: 107728

Are you sure Image.first is returning this record that you just created. Double check that it contains the correct photo_* fields.

Upvotes: 0

Related Questions