Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 2 - Controlling Information Access with the Ra...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Controlling Information Access with the Rails Action Controller
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-02-14

    Table of Contents:
  • Controlling Information Access with the Rails Action Controller
  • 4.13 Sending Files or Data Streams to the Browser
  • 4.14 Storing Session Information in a Database
  • 4.15 Tracking Information with Sessions
  • 4.16 Using Filters for Authentication

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Controlling Information Access with the Rails Action Controller - 4.13 Sending Files or Data Streams to the Browser


    (Page 2 of 5 )

    Problem

    You want to send e-book contents directly from your database to the browser as text and give the user the option to download a compressed version of each book.

    Solution

    You have a table that stores plain text e-books:

    db/schema.rb:

      ActiveRecord::Schema.define(:version => 3) do

        create_table "ebooks", :force
    => true do |t|
         
    t.column "title", :string
         
    t.column "text", :text
        end
      end

    In the DocumentController, define a view that calls send_data if the :download parameter is present, and render if it is not:

    app/controllers/document_controller.rb:

      require 'zlib'
      require 'stringio' 

      class DocumentController
    < ApplicationController

        def view
          @document = Ebook.find(params[:id])
          if (params[:download])
            send_data compress(@document.text),
                      :content_type =>
    "application/x�gzip",
                      :filename =>
    @document.title.gsub(' ','_') + ".gz"
          else
            render :text => @document.text
          end
        end

        protected
         
    def compress(text)
            gz = Zlib::GzipWriter.new(out = StringIO.new)
            gz.write(text)
            gz.close
            return out.string 
          end
       end

    Discussion

    If the view action of the DocumentController is invoked with the URL http://railsurl.com/document/ view/1, the e-book with an ID of 1 is rendered to the browser as plain text.

    Adding the download parameter to the URL, which yields http://railsurl.com/document/view/1?download=1, requests that the contents of the e-book be compressed and sent to the browser as a binary file. The browser should download it, rather than trying to render it.

    There are several different ways to render output in Rails. The most common are action renderers that process ERb templates, but it’s also customary to send binary image data to the browser.

    See Also

    More Ruby-on-Rails Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Rails Cookbook," published by O'Reilly. We...
     

    Buy this book now. This article is excerpted from chapter four of the Rails Cookbook, written by Rob Orsini (O'Reilly, 2007; ISBN: 0596527314). Check it out at your favorite bookstore. Buy this book now.

    RUBY-ON-RAILS ARTICLES

    - Calculating Statistics with Active Record
    - Creating Graphs with Ruby
    - Callbacks and the Active Record
    - Validation and the Active Record
    - Arrays, Associations and the Active Record
    - Associations and Dependencies with Active Re...
    - Advanced Active Record: Enhancing Your Models
    - Load Balancing Databases with Rails
    - More Advanced Database Features and Rails
    - Handling Advanced Database Features with Rai...
    - Managing Database Files with Ruby on Rails
    - Databases and Ruby on Rails
    - Updating and Deleting with the Active Record
    - Rails Active Record and CRUD Functions
    - Working with a Database: Active Record







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek