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
How to Match the Last Occurrence of a Pattern
Ssl Error When Installing Rubygems, Unable to Pull Data from 'Https://Rubygems.Org/
How to Test If a String Is Basically an Integer in Quotes Using Ruby
Look Up All Descendants of a Class in Ruby
Gem Installation Error: You Have to Install Development Tools First (Windows)
Converting Camel Case to Underscore Case in Ruby
Sort Hash by Key, Return Hash in Ruby
Add a Default Value to a Column Through a Migration
How to Get the Current Absolute Url in Ruby on Rails
Difference Between "And" and && in Ruby
What Does the "===" Operator Do in Ruby
Your Ruby Version Is 2.0.0, But Your Gemfile Specified 2.1.0
Installing Bootstrap 3 on Rails App
Why Does String Interpolation Work in Ruby When There Are No Curly Braces