In this article I’d like to present you some of my favourite Rails helper methods. Just add them to your app/helpers/application.rb file to use in your own application.
Booleans
This is maybe one of the most basic helpers you’ll ever encounter. But it also helps you DRY up your code, e.g. if your application is multilingual.
1
2
3
|
def yes_no(bool)
bool ? "yes" : "no"
end |
Numbers
The following helper prints numbers with correct delimiters and a fitting form of title.
1
2
3
|
def print_number(title, number)
number_with_delimiter(number) + " " + title.pluralize(number)
end |
Page title
The title helper helps to DRY up page titles:
1
2
3
4
5
|
def title(page_title)
content_for(:title) {
page_title
}
end |
I found this helper on Railscasts.
In app/views/layouts/application.rhtml I use the following two snippets:
|
<title>MySite :: <%= yield(:title) || "My uber cool site!" %></title> |
1
2
3
|
<% if yield(:title) %>
<h1><%= yield(:title) %></h1>
<% end %> |
To set the title, I call the title helper in my views like this:
|
<% title "User profile of #{@user.login}" %> |
Forms
The tabindex attribute of form elements specifies the position in the tabbing order. The tabbing order defines the order in which elements will receive focus when navigated by the user via the keyboard.
Instead of specifying the tabindex yourself, you can use the helper method
1
2
3
4
|
def autotab
@current_tab ||= 0
@current_tab += 1
end |
in your views like this:
|
<%= f.text_field :first_name, :tabindex => autotab %> |
Ruby on Rails 2.0 added the very handy label form helper. But unfortunately there is no label_tag helper. So I added one myself:
1
2
3
4
5
6
7
|
# Creates a label tag.
# label_tag('post_title', 'Title')
# <label for="post_title">Title</label>
def label_tag(name, text=nil, options = {})
content = (text.blank? ? nil : text.to_s) || name.to_s.humanize
content_tag('label', content, { 'for' => name }.merge(options.stringify_keys))
end |
I often use a submit button together with a link to cancel. Overriding the submit_tag helper provided by Rails, helps me to DRY up my views:
1
2
3
4
5
|
def submit_tag(value = "Save changes", options = {})
or_opt = options.delete(:or)
return super + " <span class='button_or'>" + "or" + " " + or_opt + "</span>" if or_opt
super
end |
Now I can use the following code in my views:
|
<%= f.submit 'Update', :or => link_to('Cancel', user_path(@user)), :tabindex => autotab %> |
Markaby
Markaby lets you generate HTML for views by writing ordinary Ruby code. I particulary like to use Markaby in my helpers. To simplify the use of Markaby, I’m using the following helper method:
1
2
3
|
def markaby(&block)
Markaby::Rails::Builder.new({}, self, &block)
end |
The markaby helper can be used in other helpers like this:
1
2
3
4
5
6
7
8
|
def logo_text
markaby do
span.logo do
span.blue "My"
span.green "App"
end
end
end |
The above helper generates the following HTML code:
|
<span class="logo"><span class="blue">My</span><span class="green">App</span></span> |