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)

Then simply :

user.picture_from_url ""

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

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

After than you can safely remove the old_avatar_url column.

Related Topics

Leave a reply