I’m trying to write a method to store an image from a given url, inside a ruby worker. It comes along my Rails app in which I display the object image. Here is what I’ve come up with:
@message = Message.create!
my_uploader = PhotoUploader.new
photo = open(image_url)
@message[:photo] = my_uploader.store!(photo)
@message[:photo] = my_uploader.filename
@message.save!
the PhotoUploader:
def store_dir
Rails.env.production? ? (primary_folder = "production") : (primary_folder = "test")
"#{primary_folder}/media/#{model.id}"
end
the Message class:
class Message < ActiveRecord::Base
attr_accessible :content, :photo, :user_id
mount_uploader :photo, PhotoUploader
end
The model.id returns an error in the storage path. The model is nil even after saving it, and the file is stored in #{primary_folder}/media
I think it’s that you’re not calling
store!from the model, which is whymodel.iddoesn’t work, becausemodelisnilThis will probably do it for you:
I tried this out in the rails console, and
@message[:photo].store!(photo)gave the same error, but@message.photo.store!(photo)worked.