Seeding File Uploads with Carrierwave, Rails 3

Seeding file uploads with CarrierWave, Rails 3

Turns out the documentation for CarrierWave is slightly wrong. There is a more up to date piece of code in the README at the GitHub repository for the project.

In a nutshell, though:

pi = ProductImage.create!(:product => product)
pi.image.store!(File.open(File.join(Rails.root, 'test.jpg')))
product.product_images << pi
product.save!

Seeding remote images Carrierwave

You should pass I/O objects to CarrierWave uploader attributes

In the local example, you are calling .open on the file path which reads the file and returns a File object.

In the remote example, you are just passing URI paths, which are strings, not an I/O object like File.

Reading a remote file

To read a remote file straight from a URI path, you can use the open method provided by the OpenURI module.

open("https://s3-eu-west-1.amazonaws.com/mysite/images_site/seeds/image_1.jpg”)

Read the documentation here:

https://ruby-doc.org/stdlib-2.1.0/libdoc/open-uri/rdoc/OpenURI.html

NOTE: You may have to add require "open-uri" if you aren’t using Rails

Seeding multiple remote files using CarrierWave

You can fix the Heroku example by wrapping the remote URI strings with open like so:

Article.create!( 
title: Faker::Music.instrument,
description: Faker::Lorem.paragraph(3),
attachments:
[ open("https://s3-eu-west-1.amazonaws.com/mysite/images_site/seeds/image_1.jpg”),
open("https://s3-eu-west-1.amazonaws.com/mysite/images_site/seeds/image_2.jpg”)]
)
end

CarrierWave - Seeding existing files

To update the database directly and skip out on CarrierWave:

artist[:picture] = 'picture.jpg'
artist.save

Rails 4 - Seed images from hard drive with carrierwave

You have to specify the full path to the image:

File.open(Rails.root + "app/assets/images/RF_web_about_us_1.jpg")

carrierwave multiple file uploads and storing

Add the relevant attributes to your model and introduce a before_save callback.

class Video < ActiveRecord::Base
mount_uploader :video, VideoUploader

before_save :update_video_attributes
private

def update_video_attributes
if video.present? && video_changed?
self.content_type = video.file.content_type
self.file_size = video.file.size
end
end
end

For more details see github

CarrierWave::FormNotMultipart error when creating records using seed.rb file

Presuming that your Gem model has the image field as a Carrierwave uploader (that is, you've got mount_uploader :image, ImageUploader or similar in your Gem model), you can assign a ruby File object to your image attribute, not a string. Something like this:

gem = Demogem.new(data)
image_src = File.join(Rails.root, "/public/images/test2.png")
src_file = File.new(image_src)
gem.image = src_file
gem.save

In your overall code, you could either change your seed data (so your image property in your hash was set to File.new(File.join(Rails.root, "/public/images/test1.jpg")) or change the way you construct your new records so it doesn't use the image by default:

gems.each do |user, data|
gem = DemoGem.new(data.reject { |k, v| k == "image" })
gem.image = new File(File.join(Rails.root, data["image"]))

unless DemoGem.where(name: gem.name).exists?
gem.save!
end
end

The CarrierWave wiki has some documentation on this, but it's not very extensive.



Related Topics



Leave a reply



Submit