9.4. Helpers

Whereas in ASP.NET you have controls that you can drag and drop to help ease the development process, keep you productive, and maintain relatively lean pages, in Rails you have helpers. These handy methods provide a convenient way to write HTML controls, as well as perform other operations on a given input in order to provide the required output. For example, whereas in ASP.NET you have the following line of code:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

in Rails, you would use the text_field or text_field_tag helper, depending on whether or not the resulting input tag needs to be bound to a particular model attribute.

Rails defines many helpers besides form ones. Consider, for instance, the pluralize and excerpt textual helpers:

<%= excerpt(@article, 'sed', 50) %>

<%= pluralize(@people.size, 'person') %>

This will display output such as the following:

...lis porttitor. Curabitur elementum risus eu eros. Sed elit. Praesent elit sapien, dictum ac,
ornare por...

3 people

Helpers offer multiple advantages. In particular, they encourage reuse, simplify the templates, ease development, make testing easier, adhere to the DRY principle, and discourage the inclusion of application logic within the view templates. Their usage is more than encouraged. Two types of helpers exist: those included by ActionView and those defined by the developer.

9.4.1. Predefined Helpers

Helpers defined by Rails are included in the module ActionView::Helpers. Consulting the API ...

Get Ruby on Rails® for Microsoft Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.