Upload image via paperclip via passing URL instead of file upload
# Consider Print instance with image as file attachment
class Print < ActiveRecord::Base
has_attached_file :image
def upload_image(url)
begin
io = open(URI.escape(url))
if io
def io.original_filename; base_uri.path.split('/').last; end
io.original_filename.blank? ? nil : io
p.image = io
end
p.save(false)
rescue Exception => e
logger.info "EXCEPTION# #{e.message}"
end
end
end
Text code from console
p = Print.new
url = "http://ocdevel.com/sites/ocdevel.com/files/images/rails.png"
p.upload_image(url)