Home arrow Ruby-on-Rails arrow Page 3 - Understanding Action Views in Ruby-on-Rails
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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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