views:

1197

answers:

3

I am writing a Rails app that processes data into a graph (using Scruffy). I am wondering how can I render the graph to a blog/string and then send the blog/string directly to the the browser to be displayed (without saving it to a file)? Or do I need to render it, save it to a file, then display the saved image file in the browser?

+6  A: 

I think you will be able to use send_data for this purpose:

send_data data_string, :filename => 'icon.jpg', :type => 'image/jpeg', :disposition => 'inline'

If you put this in a controller action - say show on a picture controller, then all you need do is include the following in your view (assuming RESTful routes):

<%= image_tag picture_path(@picture) %>
Mr. Matt
Matt, how do I reference this file in the view?
Josh Moore
A: 

I wonder if sending direct to the browser is the best way? If there is the possibility that users will reload the page would this short circuit any cache possibilities? I ask because I really don't know.

srboisvert
A: 

"If there is the possibility that users will reload the page would this short circuit any cache possibilities?"

No - whether you're serving from a file system or send_data doesn't matter. The browser is getting the data from your server anyway. Just make sure you've got your HTTP caching directives sorted out.