Save image from URL by paperclip

khanh picture khanh · Oct 29, 2010 · Viewed 57.6k times · Source

Please suggest me a way to save an image from an URL by Paperclip.

Answer

Aditya Sanghi picture Aditya Sanghi · Jul 20, 2012

In Paperclip 3.1.4 it's become even simpler.

def picture_from_url(url)
  self.picture = URI.parse(url)
end

This is slightly better than open(url). Because with open(url) you're going to get "stringio.txt" as the filename. With the above you're going to get a proper name of the file based on the URL. i.e.

self.picture = URI.parse("http://something.com/blah/avatar.png")

self.picture_file_name    # => "avatar.png"
self.picture_content_type # => "image/png"