How can I download an internet image from a URL and save it in locally in Rails?
i give a form to add image url form link if i submit that form i want to download image to computer local disk (Home/Documents/food_img)
pls need help
in downloads controller
class DownloadsController < ApplicationController
def index
@downloads = Download.all
@download = Download.new
end
def create
@download = Download.create(user_params)
end
private
def user_params
params.require(:download).permit(:image,:image_url)
end
end
in view/index.html.erb
<%= form_for(@download, url: downloads_path) do |f| %>
<%= f.text_field :image_url %>
<%=f.submit%>
<% end %>
in model
class Download < ApplicationRecord
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
require 'open-uri'
download = open('http://www.shopprod.com/assets/photos/ishop-718755d2bc62956994c867681b2622e77b4c9af3d1ecd6fa856127b704a459b2.png')
IO.copy_stream(download, "~/#{download.base_uri.to_s.split('/')[-1]}")
end
in db
t.string: image_url