Friday, January 29, 2010

Helpers usage in ROR

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.


def my_helper(opts={}) #args defined as hash
"output something" if opts[:method_passed_in] == "foo"
end

You can then use it in your view with:


<%= my_helper('foo') %>

No comments: