respond to REST-request with image-file (no XML etc.)

Hi!

I'm using REST to serve content (images and SWFs) to a flash-file. Depending on the IP of the user, I would like to serve different content. To get the IP I use 'request.remote_ip, followed by an if statement to return the correct content.

What I don't know is how do I serve just the content (no XML, just content.jpg) as a reply from my Rails-controller? The content is not stored in a database, but in my public directory.

  def show

    ip = request.remote_ip

    if ip=x       return '/content/hello.jpg'     else       return '/content/goodbye.jpg'     end

    respond_to do |format|       format.html # bbprivat.html.erb       format.xml { render :xml => @ip }     end

  end

Many thanks for all the help I can get!

Hi Gustav,

you can use the send_file command to return binary data to the client

e.g. respond_to do |format| format.html { ip == x ? send_file '/content/hello.jpg', :filename => 'hello.jpg' : send_file '/content/goodbye.jpg', :filename => 'goodbye.jpg' } end

think that will work , you might want to register a new content type for the respond_to block so it 'responds_to' .jpg as well.

Gustav Stockholm wrote:

respond_to do |format| format.html { ip == x ? send_file '/content/hello.jpg', :filename => 'hello.jpg' : send_file '/content/goodbye.jpg', :filename => 'goodbye.jpg' } end

I've tried:

def show   ip = request.remote_ip   pattern = x

  if pattern.match(ip)     @file = '/folder/y.swf'   else     @file = '/folder/x.swf'   end

  respond_to do |format|     format.html { send_file @file }   end end

This returns a returns a "Cannot read file /folder/y.swf" error message.

I've also tried:

respond_to do |format|   format.html { render :file => @file } end

This returns a "No such file or directory - /folder/y.swf", although "http://localhost:3000/folder/y.swf" works just fine.

When substituting the @file-construct for:

format.html { send_file '/folder/y.swf', :filename => 'y.swf' }

but get the same result...

think that will work , you might want to register a new content type for the respond_to block so it 'responds_to' .jpg as well.

Where do I register new content types in the respond_to-block? I'm a REST-noob, in case that wasn't obvious :wink:

Many thanks!

g.

This returns a returns a "Cannot read file /folder/y.swf" error message.

I solved it by using the full system-file path:

render :file => '/Users/gustav/Sites/sandbox/public/folder/y.swf'

Anyone know how make Rails understand to use the public-folder as root?

Also, since I get plenty of weird characters on screen, rather than the actual swf-file, I suppose I need to register swf to work with the respond_to block.

send_file '/Users/gustav/Sites/sandbox/public/folder/y.swf'

works just as good for that matter, although I get prompted to save file to disk.