RailsCasts.com 100th episode!

Posted by Floyd Price Mon, 07 Apr 2008 11:09:00 GMT

As many of you will know Code Spaces is a Ruby On Rails application and like many of you guys we love Ruby and in particular Ruby on Rails :-)

Most Rails fans will be aware of Ryan Bates and the RailsCasts.com weekly free Rails screen cast, for those who are not aware of Ryan’s work, he has been recording screen casts for the rails community since March 2005 and has covered pretty much every topic you can imagine from simple form helper tips to advanced debugging and performance tips.

RailsCasts.com have this week reached 100 episodes which I’m sure you will agree is an impressive contribution to the Rails community and demonstrates an enormous amount of dedication on Ryan’s part.

Code Spaces would like to thank Ryan for this contribution and wish him every success with the next 100 episodes ;-)

To mark this fantastic milestone Ryan has set up a simple contest where anyone who contributes 5 Rails tips back to the community via a blog or the Rails Forum can win a whole bunch of excellent prizes, some of which including an IPod Touch are donated by Code Spaces which is our very small way of saying thanks to Ryan for this contribution to the Rails community.

For further details about this contest and for a complete list of prizes visit the Rails Casts contest page

Streaming Files to the Browser with Ruby on rails

Posted by Floyd Price Tue, 05 Feb 2008 13:01:00 GMT

Quite often when building a web application you need to store and send Documents, with ruby on rails, streaming files from your application to the browser is simple.


def download_file
    @document = Document.find(params[:id])
    # this is where the magic happens...
    send_file @document.file_location
end

This single line of code will stream a file from your servers filesystem directly to your browser.

The simple use described above is often enough but you have even further control over the file with the following options:

Options:

  • :filename – suggests a filename for the browser to use. Defaults to File.basename(path).
  • :type – specifies an HTTP content type. Defaults to ‘application/octet-stream’.
  • :disposition – specifies whether the file will be shown inline or downloaded. Valid values are ‘inline’ and ‘attachment’ (default).
  • :stream – whether to send the file to the user agent as it is read (true) or to read the entire file before sending (false). Defaults to true.
  • :buffer_size – specifies size (in bytes) of the buffer used to stream the file. Defaults to 4096.
  • :status – specifies the status code to send with the response. Defaults to ‘200 OK’.
  • :url_based_filename – set to true if you want the browser guess the filename from the URL, which is necessary for i18n filenames on certain browsers (setting :filename overrides this option).

Pay particular notice to the :disposition option, changing this from its default to ‘inline’ will enable you to display the contents of the file in the browser window rather than downloading it as an attachment.

def download_file
    @document = Document.find(params[:id])
    # this is where the magic happens (inline)...
    send_file @document.file_location, :disposition => "inline"
end

This is especially useful for things like word or excel documents when you know you target audience can read these in the browser.

EDGE RAILS

If you are brave enough to live on the Edge with Edge Rails you will get an extra treat for free, on Edge Rails the send_file method passes the correct information to your balancing server to allow it to stream the file rather than blocking your mongrel server.