Skip to content

Using helper methods in Ruby on Rails

Posted on 22 May 2007

This is a quick and simple intro to using helper methods in Ruby on Rails. I'll demonstrate some simple and tidy techniques for using html, strings and ruby together.

What is a helper?

Helpers are basically small snippets of code that can be called in your views to help keep your code DRY - i.e. Any code that you are repeating regularly can most likely be moved into a helper.

Using helpers is simple, each controller has it's own helper file or you can write helpers in the application helper file if it will be used across the entire application. For example a pagination helper might go in there.

Helpers look like this:

def my_helper
  some code 
end

You can then use it in your view with:

<%= my_helper %>

How to use variables with helpers

You can pass variables into them exactly how you might expect:

def my_helper(condition)
 if condition
  "do this"
 else
  "do that"
 end
end

You also might like to pass in a hash, it's as easy as:

def my_helper(opts={})
 {:arg_one => 'foo', :arg_two => 'bar'}.merge!(opts)
end 

You can refer to the hash methods passed in with something like:

def my_helper(opts={})
 "output something" if opts[:method_passed_in] == "foo"
end 

I quite like to set defaults so that my helpers can work without forcing you to pass variables, e.g.:

def my_helper(variable = nil)
  if variable
   "do this"
  else
   "do that"
  end
end

Basically variable will be nil unless it's passed in when the helper is called.

String manipulation

These string manipulation methods methods aren't exclusive to helpers, and in fact neither are the previous examples of passing variables and hashes. However, helpers are as good a place as any to demonstrate.

By default helper, methods expect ruby code. So in order to enter html or any general string output you need to stick it in quotes. As easy as:

def my_helper
  "<p>Hello world</p>"
end

If you want to stick some ruby/rails magic inside the string then you can do something like the following:

def my_helper
  "<p>Visit my #{link_to("website", "http://paulsturgess.co.uk")}</p>"
end

Note that you must use double quotes and not single ones to take advantage of #{ruby code goes here}

Often your helper will have some conditions in it that mean you will build the html output over several lines, so the following might be handy:

def my_helper
  html = ""
  if some_condition
    html << "<p>Visit my #{link_to("website", "http://paulsturgess.co.uk")}</p>"
  else
   html << "<p>Visit some other #{link_to("website", "http://google.co.uk")}</p>"
  end
end

Note that helpers will return the value of the last line evaluated.

Now sometimes I find I want to stick quite a few lines of html in a helper and rather than writing out << each time, there's a really tidy way of achieving this:

def my_helper
  html = ""
  html  = <<HTML
    <ul>
       <li>lots</li>
       <li>of</li>
       <li>html</li>
    </ul>
HTML
end

Note the final HTML line must not be indented. Unfortunately the syntax highlighter i'm using on this site doesn't understand this technique but Textmate's syntax highlighting will pick it up which is nice.

Hopefully that gives a quick and easy introduction to using helpers in Ruby on Rails.

View the snippets archive ››

1 comment made

sal commented on 19 Jul 07 at 21:44

nice intro!! cheers :-)

Got something to say?

About

Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.

Contact

my name at gmail.com

More snippets

Take a look in the archive

Need a website?

Contact my employer. Make sure to check out our portfolio of work.

Hosting

I recommend hostingrails.com