Forum: Ruby on Rails displaying images from this controllers code

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Chris G. (Guest)
on 2007-02-16 14:40

Im pretty new to rails so please excuse me if this is a trivial question
to most of you :)

Im making use of the following code that I came accross which allows me
to go and feed amazons alexa service a url and it returns a thumbnaill.

class AmazonController < ApplicationController

  layout 'default'
  def index
    require 'cgi'
    require 'openssl'
    require 'base64'
    require 'open-uri'

    access_id = 'XXXXX'
    secret_id = 'XXXXXXXXXX'

    source_url = ''

    timestamp ="%Y-%m-%dT%H:%M:%SZ")
    sig =
secret_id, 'Thumbnail' + timestamp)).strip

    url =
" +
    url << "&Signature=" + CGI.escape(sig)
    url << "&Timestamp=" + CGI.escape(timestamp)
    url << "&Url=" + source_url

      doc = open(url).read
      puts "Could not access AWS"

    m = doc.match(/\<aws:thumbnail[^\>]+exists=\"true\"\>(.+?)\<\//i)

    if m && m[1]
      thumb_url = m[1]
      thumb_url.gsub!(/\&amp;/, '&')"#{source_url}.jpg", "w") { |f| f.write
open(thumb_url).read }
      puts "Saved to #{source_url}.jpg"
    elsif m && m.match(/exists=\"false\"/)
      puts "No thumbnail for #{source_url}"
      puts "Error"


This all works fine when i fire it off as a ruby script and places the
thumbnail in the same directory as the ruby script. what I want now
though is to bring the same code to rails. I have placed it in a
controller as you can see above and I would like now to display these
thumbnails on the front end view.

How does one access this information and bring it up on the front end? I
dont entirely understand how the image is written to disk which could be
part of the problem.

This topic is locked and can not be replied to.