Home arrow Ruby-on-Rails arrow Page 2 - 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: 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.


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:

It's <%= Time.now %>

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 %>
<% end %>

is given?

Internally, the code is converted into

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

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") %>
    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.

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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials