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.
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:
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:
All the instance variables of the controller, through which the controller transfers data to the template.
All the controller objects are available as accessors. These objects include headers, params, request, response and session.
The current controller object through the attribute controller. This means that the code can call any public method defined within that controller object.
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 %>
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 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.
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
<%= number_to_phone(2125551212) %> Converts the given number to 212-555-1212
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.