开发者

Saving images with Carrierwave inside a ruby worker

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 :

def store(url)
    @object = Object.find(1)
    @object[:image] = CarrierWave::Uploader.store!(image_url)
end

It doesn't seem to work at all. Any clues? Is there another way around?

[EDIT]

Here is the current situation :

def store
  @object = Object.find(1)
  my_uploader = ImageUploader.new

  image = open("http://twitpic.com/show/iphone/xxxx.jpg") 
# or for a local file:
  image = File.open(Rails.root.join('xxxx.png'))

  @object[:image] = my_uploader.store!(开发者_运维问答image)
  @object.save!
end

The filename in the [:image] attibute is still wrong. It gives "[:store_versions!]". How do I get the filename right?

[EDIT2]

Got the filename right by adding @artwork[:image] = my_uploader.filename before save. But @object = Object.find(1) won't work. How do I access the Object class, which is inside my rails app, from the worker?


@object.image.store!(image) finally did the job!


You'll want to create a new uploader object and point it to your file

image = File.open(Rails.root.join('path', 'to', 'file.png'))
@object[:image] = YourUploader.new(image)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜