Sunday, March 11, 2007

Helper of Rails

A helper file, located in app/helpers, is created automatically for every controller you create.

Inside the helper files, you can write arbitrarily many Ruby methods; these methods are automatically accessible in your view template code. The advantage of this arrangement is that it saves you repetition, it's more encapsulated.
If you’re using a construct several times in one or more of your templates, you can write a method that generates the construct, and then call the method from the template.

Helper methods figure in Rails in two distinct related ways:
1, Rails provides you with the apps/helpers directory and file bank to encourage you to write methods that encapsulate functionality and to keep the view templates organized.
2, Rails also supplies you a large number of predefined helper methods. link_to is a perfect example: It’s a built-in Rails helper method that gives you a API to the creation of the HTML you need.
When you write helper methods, you’re adding to the stockpile of such methods that Rails has already given you.

No comments: