Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 2 - URLs, Filters and the Rails Action Control...
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  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
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

URLs, Filters and the Rails Action Controller
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-02-07

    Table of Contents:
  • URLs, Filters and the Rails Action Controller
  • 4.9 Inspecting Requests with Filters
  • 4.10 Logging with Filters
  • 4.11 Rendering Actions

  • 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

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    URLs, Filters and the Rails Action Controller - 4.9 Inspecting Requests with Filters


    (Page 2 of 4 )

    Problem

    You have taken over development of a Rails application, and you are trying to figure out how it processes requests. To do so, you want to install a logging mechanism that will let you inspect the request cycle in real time.

    Solution

    Use an after_filter to invoke a custom logging method for each request. Define a CustomLoggerFilter class:

    app/controllers/custom_logger_filter.rb:

      require 'logger'
      require 'pp'
      require 'stringio'

      class CustomLoggerFilter 

        def self.filter(controller)
          log = Logger.new('/var/log/custom.log')
         
    log.warn("params: "+controller.params.print_pretty)
        end
      end

      class Object
        def print_pretty
          str = StringIO.new
          
    PP.pp(self,str)
          
    return str.string.chop
       
    end
      end

    Install the logger in the AccountsController by passing it as an argument in a call to after_filter:

    app/controllers/accounts_controller.rb:

      class AccountsController
    < ApplicationController

        after_filter CustomLoggerFilter

        def index
         
    list
          render :action => 'list'
        end

        def list
          @account_pages, @accounts = paginate
    :accounts, :per_page => 10
        end

        def show
          @account = Account.find(params[:id])
        end

        def new
          @account = Account.new
        end

        def create
         
    @account = Account.new(params[:account])
         
    if @account.save
           
    flash[:notice] = 'Account was successfully
    created.'
            redirect_to :action => 'list'
          else
            render :action => 'new'
          end
        end

        def edit
          @account = Account.find(params[:id])
        end

        def update
         
    @account = Account.find(params[:id])
          if @account.update_attributes(params[:account])
           
    flash[:notice] = 'Account was successfully
    updated.'
            redirect_to :action => 'show',
    :id => @account
          else
            render :action => 'edit'
          end
       end

        def destroy
          Account.find(params[:id]).destroy
          redirect_to :action => 'list' 
       
    end
      end
     

    Discussion

    Rails filters allow you to do additional processing before or after controller actions. In the solution, we’ve implemented a custom logging class that is invoked after calls to any actions in the Accounts controller. Our logger opens a filehandle and prints a formatted version of the params hash for easy inspection.

    With the logger in place, you can use the Unix tail command to watch the logfile as it grows. You’ll see what happens to the params hash with every action that’s called:

      tail -f /var/log/custom.log

    For the AccountsController in the solution, you can watch the log as you list, create, and destroy accounts.

      params: {"action"=>"list",
    "controller"=>"accounts"} 
      params: {"action"=>"new",
    "controller"=>"accounts"}
      params: {"commit"=>"Create",
       "account"=>{"balance"=>"100.0", "first_name"=>"John",
    "last_name"=>"Smythe"},
       "action"=>"create",
       "controller"=>"accounts"} 

      params: {"action"=>"list",
    "controller"=>"accounts"} 
      params: {"action"=>"destroy",
    "id"=>"2", "controller"=>"accounts"}
      params: {"action"=>"list",
    "controller"=>"accounts"}

    Rails comes with a number of built-in logging facilities. This approach gives you an easy way to add logging to a controller with only one line of code. You can also limit what actions of the controller the filter is applied to.

    See Also

    Recipe 4.10, “Logging with Filters

    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 today at your favorite bookstore. Buy this book now.

    RUBY-ON-RAILS ARTICLES

    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops
    - Ruby Conditionals
    - Ruby Operators and Arrays
    - Ruby for the Newbie


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway