Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - Understanding Action Views in Ruby-on-Rail...
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  
Moblin 
JMSL Numerical Library 
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

Understanding Action Views in Ruby-on-Rails
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2006-08-15

    Table of Contents:
  • Understanding Action Views in Ruby-on-Rails
  • ActionView: Templates and Formatting Helpers
  • ActionView: Pagination and Layouts
  • Rails in the Real World

  • 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


    Understanding Action Views in Ruby-on-Rails - ActionView: Pagination and Layouts


    (Page 3 of 4 )

    Pagination

    In the first section I discussed the theory of pagination. In this section I will be discussing how RoR helps in implementing pagination. RoR implements pagination at both the controller and view levels. At the controller level, RoR’s pagination controls which rows are retrieved from the database. At the view level it displays the links required for navigation between the pages created by the decomposition of the table. For example, to paginate a list of users, the following steps are taken:

    1. At the Controller: 

      In the controller, a paginator has to be declared in this way:

      def user_list
         
      @user_pages, @users = paginate(:users, :order_by => 'name')
      end
       

      The paginator is set up using the paginate method. The parameters are the table to paginate and the field on which sorting has to be done. It returns two objects, a paginator object which in this case is @user_pages, and an object that contains a page full of users which in this case is @users. The @users is used by the view to display the pages of users. The paginator object fetches the rows from the table and populates the @users variable with the fetched rows.

    2. At the View:

      In the corresponding RHTML template, the object containing a list of users is iterated over and displayed. The pagination_links() method is used to set up the page navigation. In code it would be as follows:

      <table>
      <tr><th>Name</th></tr>
      <% for user in @users %>
      <tr><td><%= user.name %></td>
      <% end %>
      </table>
      <hr>
      <%= pagination_links(@user_pages) %>
      <hr>


    Layouts

    Layouts honor the DRY philosophy of RoR. RoR uses the layout to provide templates within templates. In simple terms, it means RoR can render pages that are nested within other rendered pages or templates. This style provides for a typical site wide frame where the site has been divided into four or five different compartments such as banner, menu, footer and so on. A simple layout would look like this:

    <html>
    <head>
    <title>Form: <%= controller.action_name %></title>
    <%= stylesheet_link_tag 'scaffold' %>
    </head>
    <body>
    <%= @content_for_layout %>
    </body>
    </html>

    Here the code is for a standard HTML layout. The thing to look for is the variable @content_for_layout. It contains the content generated by the normal rendering of an action. So if the action (i.e. controller) was:

    defmy_action
    @msg ="Hello, World!"
    end

    and the my_action.rhtml has the following code:

    <h1><%= @msg %></h1>

    Then the variable @content_for_layout would contain :

    <h1>Hello, World!</h1> 
     
    And the layout would be rendered as

    <html
      <head> 
        <title>Form: my_action</title> 
        <link href="/stylesheets/scaffold.css" media="screen" rel="Stylesheet" type="text/css"/> 
        </head>
        <body>
            <h1>Hello, World!</h1>
        </body>
    </html>

    That’s just a glimpse of the power of layout. If you remember the scaffold generated in the first part, it uses layouts to produce the output. The advanced features of layouts will be discussed in the future. The next section will look at a simple application that uses the template and formatting techniques.

    More Ruby-on-Rails Articles
    More By A.P.Rajshekhar


       · Presenting the data to the user in a palatable way is one of the major functionality...
     

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - 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






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT