21

I have CarrierWave working fine through the typical ORM setup and upload via form. I would like to figure out how to use CarrierWave outside of the form submission context. For example, when a user registers I would like to grab their gravatar and store it with CarrierWave. Here's what I have, and it does not work:

gravatar_url = "http://www.gravatar.com/avatar/#{Digest::MD5.new.update(current_user.email)}?s=512&d=identicon"

uploader = ImageUploader.new
uploader.store! gravatar_url

No error either. I've been looking around the web and have not been able to find a solution.

lucapette
  • 20,564
  • 6
  • 65
  • 59
Marc
  • 3,812
  • 8
  • 37
  • 61

3 Answers3

46

in the controller after user sign up (assuming your user image field is called simply 'image')

gravatar_url = "http://www.gravatar.com/avatar/#{Digest::MD5.new.update(current_user.email)}?s=512&d=identicon"
@user.remote_image_url = gravatar_url
@user.save 

i think this is the best way according to carrierwave docs and some searching around.

enjoy!

Nuriel
  • 3,731
  • 3
  • 23
  • 23
  • This works great. Thanks! I spent a lot of time in figuring out more complex methods. – rohitmishra Oct 31 '12 at 06:15
  • 2
    glad i could help! this is a really useful method when importing from social or any service apis. – Nuriel Dec 13 '12 at 22:56
  • I want to use this in the callback before_create because i use devise where i have no controller, but it doesn't work. – aisensiy Mar 05 '13 at 14:14
  • 1
    @aisensiy, i hope i understand your problem correctly- you can set the address up, but in order to actually upload the image you'll have to save a record to the db first. try creating a guest model, and after the user is created re-associate or recreate the image. hopefully this should lead you in the right direction: http://railscasts.com/episodes/393-guest-user-record – Nuriel Mar 07 '13 at 02:33
19

Actually you can do this using the built in remote_{attribute}_url property if you are using the active record/model helpers (see the CarrierWave railscast for the details). However, I dug around in the source code a bit to see how this actually works and it appears that even if you are not you should be able to use the following:

uploader = ImageUploader.new
uploader.download! some_remote_url
uploader.store!

Give it a try.

Chris Nicola
  • 14,384
  • 6
  • 47
  • 61
18

I've had lots of trouble trying to figure out how to get store! to work with local file paths. It turns out that store! actually takes a file as a parameter, not a string.

For the URL, you'll need to require 'open-uri' first, then open the file/url. Something like this should work:

require 'open-uri'
gravatar_url = "http://www.gravatar.com/avatar/#{Digest::MD5.new.update(current_user.email)}?s=512&d=identicon"
tempfile = open(gravatar_url)    

uploader = ImageUploader.new
uploader.store! tempfile

The same will work with a file path, but you don't have to require open-uri in that case.

keithepley
  • 4,760
  • 3
  • 23
  • 41