-3

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
adarsh
  • 306
  • 5
  • 16
  • Possible duplicate of [How can I download a file from a URL and save it in Rails?](https://stackoverflow.com/questions/2515931/how-can-i-download-a-file-from-a-url-and-save-it-in-rails) – anothermh Jan 14 '19 at 15:58
  • @tadman need help pls – adarsh Jan 15 '19 at 07:28
  • @anothermh need help pls – adarsh Jan 15 '19 at 08:06
  • 1
    Welcome to stackoverflow. If you need help then you need to ask a specific question. It's not enough to say you're confused and post a bunch of code. Please read [this](https://stackoverflow.com/help/how-to-ask). – anothermh Jan 15 '19 at 08:20
  • @anothermh i edit my question ,pls need help – adarsh Jan 15 '19 at 13:06

1 Answers1

1

If you are using ActiveStorage (which I would recommend) you can use .attach to attach an IO stream to a record.

given:

class Download < ApplicationRecord
  has_one_attached :image
end

From the rails console you can download a file and attach it with:

require 'open-uri'
uri = 'http://www.shopprod.com/assets/photos/ishop-718755d2bc62956994c867681b2622e77b4c9af3d1ecd6fa856127b704a459b2.png'
download = Download.new
download.image.attach(io: open(uri), filename: uri.split('/').last)

However you can't just toss it whilly-nilly into your model class and expect it to work. The class body is executed when the class is read.

Instead you need to put it into a method and call it at the correct point.

class Download < ApplicationRecord
  has_one_attached :image
  def download_image!
    require 'open-uri'
    image.attach(
      io: open(self.image_url), 
      filename: self.image_url.split('/').last
    )
  end
end

class DownloadsController < ApplicationController
  def create
    @download = Download.new(download_params)
    if !@download.image.attached? && @download.image_url
      @download.download_image!
      # @todo handle errors 
    end
    if @download.save
      redirect_to @download
    else 
      render :new
    end
  end

  def download_params
    params.require(:download)
          .permit(:image, :image_url)
  end
end
max
  • 96,212
  • 14
  • 104
  • 165
  • i install activestorage using(rails active_storage:install) and then give a form to add url <%= form_for(@download, url: downloads_path) do |f| %> <%= f.text_field :image_url %> <%=f.submit%> <% end %> but it not storing image,It storing only image data tanks for help me, but i want image should store local dick(computer). – adarsh Jan 15 '19 at 07:23
  • https://guides.rubyonrails.org/active_storage_overview.html#disk-service – max Jan 15 '19 at 13:35