Using Paperclip Within Seeds.Rb

Using Paperclip within seeds.rb

Rather than setting the asset columns directly, try leveraging paperclip and setting it as ruby File object.

Image.create({
:id => 52,
:asset => File.new(Rails.root.join('path', 'to', 'somefile.jpg')),
:product_id => 52
})

Seeding a model with a paperclip image in ruby on rails

You can just create ActiveRecord Model as below.

# create Event record with paperclip file
Event.create(name: 'event1', photo: File.new("image/to/path.jpg"))

This is Event model:

class Event < ActiveRecord::Base
has_attached_file :photo
end

Upload Image From seed file using paperclip gem

i solve the error

  1. you create a image folder in your Dir.
  2. you assign root_path for image in to seed file

product_image: File.new("#{Rails.root}/images/seed3.jpg"))

How to use Seed data with Paperclip + S3

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

Seed with paperclip using image url

Here is how I'm using it:

class User < ActiveRecord::Base
has_attached_file :avatar, ...
end

user = User.new
user.avatar = URI.parse(url_goes_here)
user.save

Attachment downloaded from a URL

How to seed Rails db with images from the asset pipeline (paperclip)?

Public Images

If you are referencing an image in the public folder, store the root-relative path to the image as a string in the database. (E.g., if your image file is project/public/images/pic.jpg, then you would have image: '/images/pic.jpg' in the seeds.

Asset Managed Images

If you are referencing an image in your assets, things can get a touch more complicated (unless you’ve ill-advisedly turned off the config.assets.digest config). You’ll want to use the path to the asset in the seeds file (like with the first part of this answer referencing files in the public folder), but then when you want to link to the image in your views you’ll have to use Rails’ image_tag to get the asset-managed url for the image.

If for some reason you want to be able to get the asset-managed path to the image in any way other than image_tag in views, it gets a bit complicated. You can find some more details on working with the asset pipeline in the official Rails guide to The Asset Pipeline.

Images stored in Database

If you are storing the image file data in the database, you would use a binary field (not string). You could load the image data with a call to File.read. E.g.:

image_data = File.read('path/to/image.jpg')
Model.create!([
{
...
image: image_data
},
...
])

You’ll need to add a custom controller method & route to deliver the image data as a file to the client (be sure to handle the mime type correctly for that).

Personally, in those cases where I’ve chosen to store raw file data in the database, I prefer to have a separate table just for the data and then join it to the main model as needed. (In this case, there would be your existing images table, and it would be joined to a table named something like image_files with just one field, aside from id):

create_table :image_files do |t|
t.binary :data
end

And then you could reference that from your images table:

t.belongs_to :image_file

(or put the reference to the images table in the image_files table.)

As to the arguments for/against storing raw file data in a database, in general it’s sub-optimal (you lose the benefits of having the web-server directly sending the files from the filesystem and have to have Rails intervene). But it can make sense in some cases (such as when you want to restrict access — e.g., to specific users).

Seed images in heroku with paperclip

Found solution.

thx this

and this

and this

with

gem 'aws-sdk'



Related Topics



Leave a reply



Submit