Save Image from Url by Paperclip

Save image from URL by paperclip

Here is a simple way:

require "open-uri"

class User < ActiveRecord::Base
has_attached_file :picture

def picture_from_url(url)
self.picture = open(url)
end
end

Then simply :

user.picture_from_url "http://www.google.com/images/logos/ps_logo2.png"

Rails Paperclip Save Images from URL into Database

You will need to download the image from the url and let paperclip do the reprocessing for you. You can find code to download the image on paperclip's wiki https://github.com/thoughtbot/paperclip/wiki/Attachment-downloaded-from-a-URL

For the sake of the example, let's say you have Users with avatars, rather than products with images and the existing field is old_avatar_url. Setup User to store an avatar, as instructed by paperclip.

User.find_each do |user|
user.avatar_remote_url = user.old_avatar_url
user.save
end

After than you can safely remove the old_avatar_url column.



Related Topics



Leave a reply



Submit