Skip to Content
Ruby on Rails® for Microsoft Developers
book

Ruby on Rails® for Microsoft Developers

by Antonio Cangiano
April 2009
Intermediate to advanced content levelIntermediate to advanced
479 pages
12h 55m
English
Wrox
Content preview from Ruby on Rails® for Microsoft Developers

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby on Rails™ 3 Tutorial: Learn Rails™ by Example

Ruby on Rails™ 3 Tutorial: Learn Rails™ by Example

Michael Hartl

Publisher Resources

ISBN: 9780470374955Purchase book