Home arrow Ruby-on-Rails arrow Page 3 - Understanding Action Views in Ruby-on-Rails

Understanding Action Views in Ruby-on-Rails

Continuing our discussion of Ruby-on-Rails, this article covers the basic services provided by the ActionView. The ActionView helps you present data to your users. Keep reading to learn some terminology and study an example application.

Author Info:
By: A.P.Rajshekhar
Rating: 5 stars5 stars5 stars5 stars5 stars / 12
August 15, 2006
  1. · Understanding Action Views in Ruby-on-Rails
  2. · ActionView: Templates and Formatting Helpers
  3. · ActionView: Pagination and Layouts
  4. · Rails in the Real World

print this article

Understanding Action Views in Ruby-on-Rails - ActionView: Pagination and Layouts
(Page 3 of 4 )


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')

    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:

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


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:

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

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:

@msg ="Hello, World!"

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

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

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.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


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