How to use Seed data with Paperclip + S3

2019-02-09 10:48发布

问题:

I'm trying to seed my database with member profiles and also member profile pictures with S3 and paperclip but it doesn't seem to be working.

I can create/edit existing members within the application to add pictures with paperclip + S3 and it works just fine but seeding it doesn't work. I have searched but can't find an answer.

回答1:

I don't know what is your exact problem but you can try something like this in your seeds.rb file :

u = User.new({:name => 'username', :email => 'user@name.fr'...})
u.avartar = File.open('/Users/myAccount/avatars/user.png')
u.save!

In your User.rb file, you must have parperclip configured to work with amazon s3

has_attached_file :avatar,
    :styles => { :large => "177x177>", :thumb => "60x60>" },
    :storage => :s3,
    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
    :path => "/avatars/:style/:id/:filename"

You could find on dogan kaya berktas blog post detail about s3.yml