Veerasundaravel's Ruby on Rails Weblog

June 29, 2010

Useful Rails View helper methods

Here few useful view helper methods, which can reduce the number of inline ruby codes in your view file.

1. Cycle:

cycle(first_value, *values)

Creates a Cycle object whose to_s method cycles through elements of an array every time it is called. This can be used for example, to alternate classes for table rows.

Example:


# Alternate CSS classes for even and odd numbers…
@items = [1,2,3,4]
<table>
<% @items.each do |item| %>
<treven”, “odd”) -%>”>
<td>item</td>
</tr>
<% end %>
</table>

2. Current Cycle:

current_cycle(name = “default”)

Returns the current cycle string after a cycle has been started. Useful for complex table highlighing or any other design need which requires the current cycle string in more than one place.

Example:


# Alternate background colors
@items = [1,2,3,4]
<% @items.each do |item| %>
<div style=”background-color:<%= cycle(“red”,”white”,”blue”) %>”>
<span style=”background-color:<%= current_cycle %>”><%= item %></span>
</div>
<% end %>

3. Excerpt:

excerpt(text, phrase, *args)

Excerpts the given phrase that matches first instance of phrase.

Examples:

excerpt(‘This is an example’, ‘an’, :radius => 5)
# => …s is an exam…

excerpt(‘This is an example’, ‘is’, :radius => 5)
# => This is a…

excerpt(‘This is an example’, ‘is’)
# => This is an example

excerpt(‘This next thing is an example’, ‘ex’, :radius => 2)
# => …next…

excerpt(‘This is also an example’, ‘an’, :radius => 8, :omission => ‘<chop> ‘)
# => <chop> is also an example

4. Truncate:

truncate(text, *args)

Truncates a given text after a given :length if text is longer than :length (defaults to 30).

Examples:

truncate(“Once upon a time in a world far far away”)
# => Once upon a time in a world…

truncate(“Once upon a time in a world far far away”, :length => 14)
# => Once upon a…

truncate(“And they found that many people were sleeping better.”, :length => 25, “(clipped)”)
# => And they found t(clipped)

truncate(“And they found that many people were sleeping better.”, :omission => “… (continued)”, :length => 25)
# => And they f… (continued)

5. Pluralize:

pluralize(count, singular, plural = nil)

Attempts to pluralize the singular word unless count is 1. If plural is supplied, it will use that when count is > 1, otherwise it will use the Inflector to determine the plural form

Examples:

pluralize(1, ‘person’)
# => 1 person

pluralize(2, ‘person’)
# => 2 people

pluralize(3, ‘person’, ‘users’)
# => 3 users

pluralize(0, ‘person’)
# => 0 people

6.Word Wrap:

word_wrap(text, *args)

Wraps the text into lines no longer than line_width width. This method breaks on the first whitespace character that does not exceed line_width (which is 80 by default).

Examples:

word_wrap(‘Once upon a time’)
# => Once upon a time

word_wrap(‘Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding a successor to the throne turned out to be more trouble than anyone could have imagined…’)
# => Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding\n a successor to the throne turned out to be more trouble than anyone could have\n imagined…

word_wrap(‘Once upon a time’, :line_width => 8)
# => Once upon\na time

word_wrap(‘Once upon a time’, :line_width => 1)
# => Once\nupon\na\ntime

Reference link:

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html

Advertisements

%d bloggers like this: