4

I have a form to add image url form, If user adds image url I want to download the image and save to local disk(home/documents/image)

I create image model and controller..

need help pls

in image model

class Image < ApplicationRecord

    has_one_attached :image
    require 'open-uri'
    image = open('http://example.com/image.png')
    IO.copy_stream(image, '~/home/Documents/Images/local_image.png')

end

in image controller

class ImagesController < ApplicationController 

    def index
      @image = Image.new
    end

end

in image/index.html.erb

<%= form_for(@image, url: images_path) do |f| %>
  <%= f.text_field :image_url %>
  <%=f.submit%>
<% end %>

but getting error

OpenURI::HTTPError in ImagesController#index 404 not found
Tanay Sharma
  • 1,098
  • 10
  • 29
adarsh
  • 306
  • 5
  • 16
  • Please add code for `image` model and controller and view also (form). – Tanay Sharma Jan 21 '19 at 07:12
  • The question doesn't appear to include any attempt at all to solve the problem.StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, and show a specific roadblock you're running into with a [Minimal, Complete, and Verifiable](https://stackoverflow.com/help/mcve) example. For more information, please see [How to Ask](https://stackoverflow.com/help/how-to-ask) – JIJOMON K.A Jan 21 '19 at 07:16
  • 2
    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) – Tanay Sharma Jan 21 '19 at 07:17
  • In form submitting action get inserted URL and then apply logic to download it. – Tanay Sharma Jan 21 '19 at 08:21
  • <%= form_for(@image, url: images_path, method: :get ) do |f| %> <%= f.text_field :image_url %> <%=f.submit%> <% end %> – adarsh Jan 21 '19 at 08:38
  • it get url but it not download image to local disk – adarsh Jan 21 '19 at 08:39
  • Your index action does not have any logic to download image from form. Why did you add logic with static content in model ? – Tanay Sharma Jan 21 '19 at 08:42

1 Answers1

2

Try with this

ImagesController

require 'open-uri'

def get_image_url
end

def download_image
  read_image = open(params[:image_url]).read
  File.open('/home/documents/image/image.png', 'wb') do |file|
    file.write read_image
  end
  redirect_to root_path
end

Create a new file in your images view get_image_url.html.erb

<%= form_tag("/images/download_image", method: "post") do  %>
  <%= label_tag :image_url %>
  <%= text_field_tag :image_url %>
  <%= submit_tag 'Download' %>
<% end %>

routes.rb

post 'images/download_image'
get 'images/get_image_url'

Note: If you want to give different name to images make a method and pass it at image name and modify code (action name and files) according to your need.

Edit: To get current folder path

pwd

Tanay Sharma
  • 1,098
  • 10
  • 29