Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 2 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 / 9
    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: Templates and Formatting Helpers


    (Page 2 of 4 )

    ActionView, as the name suggests, provides all the necessary services for building a dynamic View component. The following are the main services provided by the ActionView:

    1. Templates
    2. Formatting Helpers
    3. Pagination
    4. Layouts

    The technique of cache-control at the View level will be discussed in the future. Except for helpers, all other services/functionalities provided by ActionView are along the lines of the concepts discussed in the previous section.

    Templates

    As discussed before, templates contain a mixture of code and static/fixed markup or text. It is the code that provides the dynamic content by accessing the information that has been set up by the controller. The code accesses the information through the environment in which it is executed. The environment provides the following information to the code:

    1. All the instance variables of the controller, through which the controller transfers data to the template.
    2. All the controller objects are available as accessors. These objects include headers, params, request, response and session.
    3. The current controller object through the attribute controller. This means that the code can call any public method defined within that controller object.
    4. The path to the root or base of the template. This is available in the base_path attribute.



    The most common form of template used in RoR is RHTML. In simple terms, RHTML is an HTML file with dynamic content. The dynamic content is generated using the inline coding technique. For example, the following code displays the current date and time:

    <h1>Hello!</h1>
    <p>
    It's <%= Time.now %>
    </p>

    Any code between the <%= %> is evaluated and the resulting value is converted to a string using the to_s() method. Any code or logic can come between <%%>. But one should not embed business logic in such a manner because View is not the place for business logic. So what really happens when a code such as

    <% 3.times do %>
        Ho!<br/>
    <% end %>

    is given?

    Internally, the code is converted into

    3.times do
        puts "Ho!<br/>"
    end


    That’s about it for templates for now. The advanced techniques associated with templates will be discussed in the future. Now let's look at formatting helpers.

    Formatting Helpers

    Formatting the data transferred from the controller into a form understandable by the user is one of the main logics in, and functions of, the View component. RoR provides helpers for the same. There are two main sets of formatting helpers. They are number and date formatting, and text formatting. As the names suggest, the former aids in formatting numbers and dates, while the latter helps in formatting the text.

    1. Number and Date Formatting:

      These helpers provide formatting such as localized time in words, converting numbers to currency, percentage and phone numbers. Following are some of the examples:

      <%= distance_of_time_in_words(Time.now, Time.local(2005, 12, 25)) %>
      Displays 248 days

      <%= distance_of_time_in_words(Time.now, Time.now + 33, false) %>
      Displays 1 minute

      <%= distance_of_time_in_words(Time.now, Time.now + 33, true) %>
       
      Gives result as half a minute

      <%= human_size(123_456) %>
      Displays 120.6 KB

      <%= number_to_currency(123.45) %> 
       Gives $123.45

      <%= number_to_currency(234.56, :unit => "CAN$", :precision => 0) %>
      Returns CAN$ 235.

      <%= number_to_percentage(66.66666) %>
      Gives 66.667%

      <%= number_to_percentage(66.66666, :precision => 1) %>
      Displays 66.7%

      <%= number_to_phone(2125551212) %>
      Converts the given number to 212-555-1212


    2. Text Formatting:

      As the name suggests, these helpers assist with formatting text. The functionalities include truncating strings, highlighting words in a string, pluralizing words and providing simple Word-like formatting. The following are examples of such functionalities:

      <%= excerpt(@trees, "lovely", 8) %>

      Returns...A poem lovely as a tre...
      <%= highlight(@trees, "tree") %>
      displays 
      think that I shall never see
      A poem lovely as a <strong class="highlight">tree</strong>.


      where tree is the instance variable defined in the corresponding controller and contains “I think that I shall never see A poem lovely as a tree” as the  value. The following shows pluralization:   

      <%= pluralize(1, "person") %> but <%= pluralize(2, "person") %>
      Returns 1 person but 2 people.


    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-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT