title: How do I download a remote image from another site to a file_column in Ruby on Rails?
body: <p>first question, hopefully I don't mess it up :)</p>
  
  <p>A bit of a Ruby on Rails newbie (also Ruby newbie) and have stumbled upon a problem with the intended behavior of the application.</p>
  
  <p>I have a file_column :image in model picture that belongs to model product, which can have many pictures.</p>
  
  <p>The file_column works just fine when used as I think it's meant to be used and that's for uploading image using &lt;%= file_column_field "picture", "image" %> etc. That part works just fine.</p>
  
  <p>The problem comes with the intention of having a text field where user can enter a css -selector for an image tag on their site (they've registered the site and the path to the page where the image should be). I haven't been able to figure out how to properly download the image from that other site "under the hood".</p>
  
  <p>Using these two methods both result in <em>Do not know how to handle a string with value 'GIF89ad.....</em> followed by loads of "binary".</p>
  
  <p>Method 1:</p>
  
  <pre><code>url = URI.parse(picture_www.external_url)
  Net::HTTP.start(url.host, url.port) {|http|
    resp = http.get(url.path)
    picture_www.image = resp.body unless resp.nil?
  }
  </code></pre>
  
  <p>Method 2:</p>
  
  <pre><code>res = open(picture_www.external_url)
  picture_www.image = res.read unless res.nil?
  </code></pre>
  
  <p>The external_url contains the correct url and the download goes ok, so the problem seems to be in the way I'm trying to assign the image to the file_column field. Naturally the problem could be the way I'm downloading the image, I have no idea TBH where the problem actually lies... :)</p>
  
  <p>Anyone able to help me please?</p>
  
  <p>Update:</p>
  
  <p>Trying to use a tempfile "causes undefined method 'original_filename' for" etc</p>
  
  <pre><code>  Net::HTTP.start(url.host, url.port) {|http|
      resp = http.get(url.path)
      tempfile = Tempfile.new('test.jpg')
      File.open(tempfile.path, 'wb') do |f|
        f.write resp.body
      end
      picture_www.image = tempfile unless resp.nil?
    }
  </code></pre>
  
  <p>Update2:</p>
  
  <p>Debugging shows me that an uploaded file has attributes @content_type ("image/jpeg" for instance) and @original_path (file name without path) under @_dc_obj and @tmpfile when the tempfile I created does not. Setting these properly would perhaps make this work? How do I set those properly? And if setting those values properly, would the file downloading be done "properly"? After ofcourse re-structuring the code once I get a working solution.</p>
  
  <p>Update3:</p>
  
  <p>From Minver's answer I got the solution for "original_filename" issue and this code seems to work:</p>
  
  <pre><code>  io = open(picture_www.external_url)
    def io.original_filename; base_uri.path.split('/').last; end
    io.original_filename.blank? ? nil : io
    picture_www.image = io
  </code></pre>
  
  <p>No idea though, if this is the "proper" way to do this or not, but this is what I'll be using for now unless some "clearly the right way to do it" solution appears :)</p>
  
  <p>-Pkauko</p>