Home arrow Ruby-on-Rails arrow Page 2 - Controlling Information Access with the Rails Action Controller
RUBY-ON-RAILS

Controlling Information Access with the Rails Action Controller


In this conclusion to a four-part series on the Rails Action Controller, you will learn how to restrict access to controller methods, use filters for authentication, and more. This article is excerpted from chapter four of the Rails Cookbook, written by Rob Orsini (O'Reilly, 2007; ISBN: 0596527314). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
February 14, 2008
TABLE OF CONTENTS:
  1. · Controlling Information Access with the Rails Action Controller
  2. · 4.13 Sending Files or Data Streams to the Browser
  3. · 4.14 Storing Session Information in a Database
  4. · 4.15 Tracking Information with Sessions
  5. · 4.16 Using Filters for Authentication

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials