A Catalog of Helper Methods: Appendix D - Learning Rails
by Edd Dumbill, Simon St. LaurentEveryone who has used Rails for a while has their own set of
“commonly used” helper methods. Many times, though, those commonly used
sets are different. Some people use FormHelper for all of their forms, while others
prefer FormTagHelper. Some people use
AssetTagHelper, while others handcode
links to static resources like stylesheets and images.
This excerpt is from Learning Rails . Most Rails books are written for programmers looking for information on data structures. Learning Rails targets web developers whose programming experience is tied directly to the Web. Rather than begin with the inner layers of a Rails web application -- the models and controllers -- this unique book approaches Rails development from the outer layer: the application interface. You can start from the foundations of web design you already know, and then move more deeply into Ruby, objects, and database structures.
Rather than provide a comprehensive reference to these methods—the API documentation does that—this appendix provides a catalog you can browse to decide which methods might actually prove useful to your own needs. Much of the difficulty in using helpers is in finding them before you reinvent the wheel yourself.
All of these classes are subclasses of ActionView::Helpers.
Note
The easiest place to find API documentation, in a friendlier form than usual, is at http://rails-doc.org/. The search boxes give you choices as you type, and the explanations are presented in smaller pieces. You can also find the documentation at http://www.railsbrain.com/ and http://www.gotapi.com/rubyrails. They’re all a little different, but hopefully one of them will prove comfortable for you.
Every helper method has its own set of parameters, and often it’s not clear from the documentation which parameters it will accept. How do you interpret the following?
label(object_name, method, text = nil, options = {})The first few parameters at least take simple values. The object_name parameter will take a symbol
pointing to an ActiveRecord object, like :person. The method parameter, though—what method does it
take? It actually wants a symbol, say :name, for an attribute from the object specified in the
previous parameter. Why would the Rails documentation call that a
method? Because it’ll use a method to access the attribute.
The next parameter, text, is
shown with its default value, nil.
Any time you see a parameter listed as equal to something, that value is
the default.
And options? What is options? It looks like lots of methods must
have the same options, because they all have the same entry in the
documentation, but it’s really just a convention. The actual options,
named parameters, are listed below in the documentation for the method.
Sometimes the options just create HTML attributes—use the name of the
attribute to create an attribute, like :id
=> 'myIDvalue'. Other times the helper methods take more
specific options that fit their particular needs. You don’t generally
need to surround the options in {},
either.
Note
For more on a case where the curly braces ({}) are necessary, see the section called “Creating Checkboxes” ” in Chapter 6, Presenting Models with Forms.
There’s also a case—with FormHelper methods in particular—where some
of the parameters disappear into a context object. See the section the section called “Form As a Wrapper” ” in Chapter 6, Presenting Models with Forms for more information
on how this works.
Sometimes you’ll also see parameters listed that begin with an
asterisk, like *sources. This means
that you can supply multiple values for that parameter.
Parameters and named parameters are enough for most helper method
calls, but every now and then you’ll see a method whose arguments end
with &block. form_for is one of the commonly used ones that
does this, but some methods take this as an option and others require
it. When you call a method with a block, however, the block doesn’t look
quite like part of the arguments:
<% benchmark "It took this long:" do %> <%= my_long_method %> <% end % >
In this case, the benchmark
method is taking two arguments. The first, a string, is “It took this
long:”; this will be text incorporated in the log. The second argument
starts with do and closes with
end, and includes everything in the
middle. That’s the block. (Blocks can also be marked with { and } in
normal Ruby code, but in the ERb where you’ll be writing helper methods,
do and end are a better choice.)
Because benchmark is keeping
track of how long it takes some code to run, it needs that code included
as an argument. The cache, capture, and content_for methods have similar needs, as do
form_for and fields_for, which surround a group of methods
and provide them context.
For developers coming from less flexible languages, Ruby’s creative use of blocks can be very difficult to figure out. If you’re feeling stuck, your best option is to work from examples until you’re ready to move forward with your own experiments.
The ActiveRecordHelper
class seems intent on providing the fastest possible path from an
ActiveRecord object to an HTML representation. These methods may be
useful for putting together very quick demonstrations or for debugging
purposes, but they aren’t likely to be your best choice for
application-building. (In general, FormHelper and FormTagHelper are better choices for
building forms.)
error_message_onReturns a
divcontaining the error message for a given object and method. You can add text before or after the message.error_messages_forReturns a
divcontaining all the error messages for a given object. (The documentation suggests that you look at the code and make your own method if you need something more specific.)formCreates a POST-based form based on the ActiveRecord object, all in one call. You can add extra pieces to the form through a block, but mostly this is good for quick-and-dirty instant forms.
inputCreates an input element based on the type of the object and method it’s passed. It’s kind of like a field-by-field version of form.
In Rails terms, an asset is something static that you want to include on a web page that isn’t controlled by Rails. These include things like stylesheets, JavaScript libraries, and sometimes images.
When working on a small scale, assets are stored in the public/ directory of your Rails application,
but you can put them on a separate server and tell Rails where to find
them through ActionController::Base.asset_host. A separate
server can speed delivery, let you share assets with other applications,
or just reduce the amount of work your Rails application has to do
itself.
The methods in AssetTagHelper
will assume files are in your public/ directory unless you’ve specified
otherwise. Most of them generate HTML tags for you, though a few let you
specify ways to generate tags in the future.
The methods you should probably focus on initially include:
auto_discovery_link_tagimage_tagjavascript_include_tagReturns a
scripttag for the JavaScript files you identify as parameters. If one of the parameters is:defaults, theapplication.jsfile will be included, bringing in the Prototype and Script.aculo.us libraries. You can provide full paths to your scripts, even scripts on other servers, or you can just provide the file’s name. If you’re feeling fancy, you can define groups of styles withregister_javascript_expansion, and reference them with symbols.stylesheet_link_tagReturns a
linktag for the CSS stylesheet files you identify as parameters. You can provide full paths to your stylesheets, even stylesheets on other servers, or you can just provide the name of the file. The:allsymbol will link all of the stylesheets in thepublic/stylesheetsdirectory. As with scripts, if you’re feeling really fancy, you can define groups of styles withregister_stylesheet_expansion, and reference them with symbols.
There are other methods in AssetTagHelper, but they’re mostly internal or
only used in special cases:
image_path(orpath_to_image)An internal method used to calculate where to point for an image.
javascript_path(orpath_to_javascript)An internal method used to calculate where to point for a JavaScript file.
register_javascript_expansionLets you register a symbol that can reference JavaScript files. Useful if you consistently use a group of script files together.
register_javascript_include_defaultLets you add JavaScript files to the
:defaultssymbol used by thejavascript_include_tagmethod.register_stylesheet_expansionLets you register a symbol that can reference style files. Useful if you consistently use a group of stylesheets together.
stylesheet_path(orpath_to_stylesheet)An internal method used to calculate where to point for a stylesheet.
Atom feeds started out as more or less the next generation of RSS syndication feeds. RSS can stand for Really Simple Syndication, Rich Site Summary, or RDF Site Summary, which is part of why Atom’s developers decided to start over with a new name. Syndication feeds make it easy for sites, especially news sites and weblogs, to share their content with other websites and consumers, offering a simpler format for articles than full HTML pages. Atom, especially its REST-based Atom Publishing Protocol (AtomPub), has grown beyond just exchanging lists of articles, but Rails’ built-in helper functions focus on fairly traditional feed applications.
AtomHelper is mostly used in
Builder templates for creating XML:
atom_feedTakes a block, creating an
atom:feedelement and giving the block an AtomFeedBuilder object that child components can use for context. It also accepts parameters for:language,:root_url,:url, and:schema_date.entryCreates an
atom:entryelement. Accepts parameters for:published, which represents the time when the entry was first published,:update, which represents the time of the latest changes, and:url, where to find the entry.updatedTakes a time and converts it to the right format for Atom feed times.
All three of these classes contain methods that wrap around content in
your templates. BenchmarkHelper is a
class you’ll mostly want to use during development, when it may help you
isolate code that’s taking the view a long time to run. CacheHelper and CaptureHelper are both
for advanced development. While CacheHelper allows you to specify fragments of your
views that will be stored for future reuse, and applied when the same
call comes through, CaptureHelper
lets you manually grab content that needs to be used again in the same
view, probably to share content from the template with the
layout:
benchmarkThe
benchmarkmethod takes an optional message argument and an optional logging level argument (:debug,:info,:warn, or:error). It records how long the wrapped code takes to run. It requires a block argument, so it usually looks something like:<% benchmark "It took this long:" do %> <%= my_long_method %> <% end % >
The message and the length of time it takes to run will end up in the logs.
cacheThe
cachemethod lets you flag fragments of your view to be kept for caching. Likebenchmark,cachewraps around the view code it’s meant to work on with a block argument:<% cache do %> <%= my_repetitive_method_that_should_be_cached %> <% end % >
You should only cache information that doesn’t change very often, but many HTML components are pretty stable.
captureThe
capturemethod wraps around view code and stores its output to a variable. You can then reference the variable and have that content appear wherever you need. In operation, it looks like a variable assignment to a method:<% @trapped_content = capture do %> <%= content_to_put_in_there %> <% end % >
Once you’ve captured it, you can reference
@trapped_contentwherever it is convenient.content_forThe
content_formethod is much likecapture, but instead of putting the content in a variable, it lets you create a named block you canyieldto in order to include the content.
The DateHelper class contains two kinds of helper methods. There’s a
small set of methods for expressing times in somewhat more
human-friendly forms:
distance_of_time_in_wordsTakes two time values and expresses how far apart they are in rough word descriptions rather than precise time notation—e.g., “2 days,” or “about 1 month,” or “less than a minute.”
distance_of_time_in_words_to_nowortime_ago_in_wordsLike
distance_of_time_in_words, but with theto_timealways set tonow.
Most of DateHelper’s methods,
though, create form fields for specifying times and dates. They’re kind
of clunky, but they may be useful for when you’re getting started or
when you feel like overriding them. Three of them are bound to
particular objects of type :date,
:time, or type :datetime:
date_selectThe
date_selectmethod creates drop-down year, month, and day select fields bound to a particular ActiveRecord object of type:date.datetime_selectThe
datetime_selectmethod creates drop-down year, month, day, hour, minute, and second select fields bound to a particular ActiveRecord object of type:datetime.time_selectThe
time_selectmethod creates drop-down hour, minute, and second select fields bound to a particular ActiveRecord object of type:time.
The rest of DateHelper’s
methods create HTML form fields, but aren’t bound to any particular
ActiveRecord object:
select_dateThe
select_datemethod creates drop-down year, month, and day select fields.select_datetimeThe
select_datetimemethod creates drop-down year, month, day, hour, minute, and second select fields.select_dayThe
select_daymethod creates a drop-down field for day of the month (1–31).select_hourThe
select_hourmethod creates a drop-down field for hours (0–23).select_minuteThe
select_minutemethod creates a drop-down field for minutes (0–59).select_monthThe
select_monthmethod creates a drop-down field for month (1–12).select_secondThe
select_secondmethod creates a drop-down field for seconds (0–59).select_timeThe
select_timemethod creates drop-down hour, minute, and second select fields.select_yearThe
select_yearmethod creates a drop-down field for year. By default it uses five years on either side of the current or selected year, but you can set start and end years through parameters.
The DebugHelper class isn’t exactly a powerful debugger, but its one
method lets you do something that’s often useful in development
mode—report an object’s contents:
These three classes of helper methods offer different approaches
to building forms and some different pieces for creating forms. Much of
the time you’ll want to use either FormHelper or FormTagHelper, but you might mix FormOptionsHelper with either of the other
two.
The FormHelper methods create form fields bound to particular attributes
of ActiveRecord objects. They are
easiest to use within a form_for
method that sets their context, as described in Chapter 6, Presenting Models with Forms. If you don’t like
that approach, you can supply an object and attribute name as the first
two parameters when calling them. (The documentation for each method
shows the parameter approach.)
check_boxCreates a checkbox field bound to an attribute from the object specified in
form_foror in the parameters. (It also creates a hidden field bound to the same attribute for use if the checkbox isn’t checked.)fields_forfields_foris likeform_for, except that it doesn’t create the actualformtags.file_fieldCreates a file upload field bound to an attribute from the object specified in
form_foror in the parameters. (You’ll need to modify theform_forcall as described in Chapter 8, Improving Forms to use this method.)form_forCreates a
formelement and sets the context for the other helper methods inFormHelper.hidden_fieldCreates a hidden field bound to an attribute from the object specified in
form_foror in the parameters.labelCreates a label for a field created with the other methods of
FormHelper.password_fieldCreates a password field bound to an attribute from the object specified in
form_foror in the parameters.radio_buttonCreates a radio button bound to an attribute from the object specified in
form_foror in the parameters.text_areaCreates a larger multiline text area bound to an attribute from the object specified in
form_foror in the parameters.text_fieldCreates a single-line text field bound to an attribute from the object specified in
form_foror in the parameters.
The FormTagHelper class does similar work but provides no automatic binding
to a single shared object for a form. It lets you build forms where each
component is specified separately:
check_box_tagLets you create checkboxes. Unlike
check_box, it doesn’t automatically create a hidden field for use if the box is unchecked.field_set_tagCreates a fieldset tag for grouping form elements. You can set the legend for the fieldset as an argument.
file_field_tagCreates a file upload tag. To use this, you also need to give the
form_tagmethod a:multipart => trueparameter.form_tagCreates a
formtag that wraps around other form elements but does not set context likeform_for.hidden_field_tagimage_submit_tagNot for submitting images, but rather for creating submit buttons that are presented as images.
label_tagpassword_field_tagradio_button_tagselect_tagsubmit_tagtext_area_tagtext_field_tag
The FormOptionsHelper
methods are complementary to the methods in the other two
form-related helper classes. Some of the methods (collection_select, country_select, select, and time_zone_select) take the same arguments as
the field-creating methods in FormHelper and can be used the same way, with
a context set by form_for or without.
The other methods are focused on creating options for those methods, and
may also be used to create option lists for the FormTagHelper’s select_tag method:
collection_selectcountry_options_for_selectCreates
optiontags for an alphabetical list of countries, accepting arguments to indicate which should be selected and which should appear first in the list.country_selectCreates a complete
selectlist of countries includingoptiontags. Also accepts arguments for a selected default and for giving countries higher priority in the list.option_groups_from_collection_for_selectCreates
optiontags structured withoptgrouptags based on an object or array.options_for_selectoptions_from_collection_for_selectselecttime_zone_options_for_selecttime_zone_selectReturns a complete
selectlist for time zones across the planet.
Sometimes your Rails code will need to generate JavaScript, and not always in the context of RJS, as described in Chapter 16, Creating Dynamic Interfaces with Rails and Ajax. These helper methods make it simpler to add basic JavaScript functionality to your pages, and remove the need to code some kinds of simple JavaScript by hand:
button_to_functionCreates a button that will call a JavaScript function using its
onclickhandler. It accepts a block of code, which works like RJS.define_javascript_functionsCreates a link to Prototype and other JavaScript files, but is best avoided. Use
javascript_include_taginstead.escape_javascriptReformats JavaScript containing carriage returns and quotes so that it can safely be put into HTML attribute values.
javascript_tagCreates a
scripttag. Again,javascript_include_tagmay be a better option.link_to_functionCreates a link that will call a JavaScript function. Like
button_to_function, it also can take a block that works like RJS.
The NumberHelper class provides convenience methods for formatting numbers:
number_to_currencyTurns a number into a currency representation. You can select the
:unit(denomination),:separator(normally.),:delimiter(normally,),:format(whether the currency comes before or after the number), and:precision(normally 2).number_to_human_sizeTurns file-size byte counts into more typical human representations, like 12 GB.
number_to_percentageTurns a number into a percent value. You can select the
:precision(normally three digits after the decimal), and the:separator(normally.).number_to_phoneTurns a number into an American-style telephone number. You can specify a country code, extension, delimiter, and whether or not the area code has parentheses, but you can’t specify how the numbers are broken down.
number_with_delimiterFormats a number with a given
:delimiterbetween thousands (,by default) and decimal:separator(.by default).number_with_precisionFormats a number to present as many digits after the decimal point as are specified in the second argument (three is the default).
The Prototype JavaScript library simplifies many common Ajax tasks, but these helper methods make it even more convenient to incorporate calls to Prototype in Rails templates:
evaluate_remote_responseEvaluates the JavaScript response from a remote service using the JavaScript
evalmethod. Theevalmethod opens JavaScript applications up to attack, so be certain that what you’re processing is free of potentially harmful code.form_remote_forform_remote_tagCreates a
formelement that uses anXmlHttpRequestcall to submit form data. This allows the page to handle the response rather than reloading an entirely new page.link_to_remoteCreates a link that issues an
XmlHttpRequestcall, again allowing the page to handle the response rather than replacing the current page with a new destination.observe_fieldWatches the content of a given field and makes a remote call (or a JavaScript function call) when the content of that field changes. Useful for components like text fields that provide suggestions.
observe_formWatches the content of a given form and makes a remote call (or a JavaScript function call) when the content of that field changes.
periodically_call_remoteMakes an
XmlHttpRequestcall every so often, according to a duration specified in seconds set as the:frequencyoption.remote_form_forWorks like
form_for, except that submitting the form triggers anXmlHttpRequestcall that gets handled by Ajax in the browser instead of the usual form submission process.remote_functionReturns the JavaScript needed to make an
XmlHttpRequestcall to a remote function.submit_to_remoteCreates a button that will submit a form using an
XmlHttpRequestcall.update_pageCreates the context for RJS calls, allowing a block of code to update multiple elements on the same page.
update_page_tagCreates JavaScript, wrapped in a
scripttag, using the same mechanisms as creating RJS code.
These methods make it easier for views to identify components referring to Rails objects when building HTML or RJS:
dom_classCreates a value suitable for a
classattribute that is the singular form of the object name.dom_idCreates a value suitable for an
idattribute that is the singular form of the object name plus_and the object’sidvalue.partial_pathCreates a value containing
plural/singularform of an object’s name, liketurtles/turtleorpeople/person.
The SanitizeHelper methods
support a variety of approaches to escaping HTML and CSS. They
complement the h method (short for
html_escape, part of the ERb:Util class) by providing other approaches
to escaping markup or letting it pass:
sanitizeThe
sanitizemethod provides a customizable approach to removing attributes and markup that you don’t want to pass through. The customization can be specified through the:tagsand:attributesparameters, or set by default through initializer code.sanitize_cssThe
sanitize_cssmethod removes features from CSS that the creators ofsanitizefelt were too dangerous. This is used bysanitizeon style attributes.strip_linksThe
strip_linksmethod leaves markup other than links intact, but removes all links from the argument.strip_tagsThe
strip_tagsmethod removes all HTML markup from the argument. (The documentation warns that it may not always find all HTML markup, however.)
Like the PrototypeHelper
methods, ScriptaculousHelper methods
provide ready Ruby-based access to JavaScript components in the browser.
While Prototype focuses on basic Ajax communications, Script.aculo.us
focuses more on actions and special effects:
draggable_elementIdentifies an HTML element, specified by
idattribute value, as supporting user efforts to drag it around the screen and potentially drop it on a receiving element.drop_receiving_elementIdentifies an HTML element, again by
idattribute value, as a container where users can drop draggable objects and expect a response. When the drop happens, the code this creates can make a remote Ajax call or a local JavaScript call, and supportsclass-based constraints on which objects to accept. It also supports changing theclassof the receiving element to give users visual feedback when they’ve positioned a draggable object over a container that could accept it.sortable_elementIdentifies an HTML element, again by
idattribute value, as a collection users can reorder, making an Ajax call when changes occur.visual_effectProvides access to the Script.aculo.us library’s collection of visual effects, applying them to an HTML element specified by
id.
TagHelper may be of use when you want to create explicit XHTML
markup using ERb templates, or want to create view logic that lets the
data determine which markup is used. On the one hand, these methods are
somewhat obscure; on the other, they may be exactly what you need if
clean XHTML is your goal:
cdata_sectionAs its name suggests, the
cdata_sectionmethod lets you create CDATA sections wrapping the content specified in the argument. (CDATA sections let you mark sections of an XML document as not containing any markup, so you can use<,>, and&to your heart’s content.)content_tagThe
content_tagmethod is a generic tag building method. You define the name of the tag, the attributes, and the content of the tag through the arguments.escape_onceThe
escape_oncemethod is extremely convenient when you have content that needs<,>, and&escaped—but you might already have done some of the escaping. This method is smart enough to escape markup text that needs escaping, while leaving the ampersands that are part of prior escaping alone.tag
The TextHelper methods offer a variety of tools for formatting,
manipulating, and presenting plain text. If you’re building blogs or
other software where users are entering content, this class is worth a
close look:
auto_linkThe
auto_linkmethod is a simple way to make links live without requiring people to use HTML. Its first argument is a block of text. By default, it will turn all URLs and email addresses in that text into live links. Thelinkparameter defaults to:all, but also accepts:email_addressesand:urlsas options if you just want one or the other. Thehref_optionsparameter lets you add attributes.(If you want to get really fancy, you can supply a block of code that will be executed for every link that gets added, letting you control processing precisely.)
concatUsed for those obscure times when you want to generate output inside of a
<% %>ERb code block instead of the usual<%= %>block.cycleCreates an object that lets you alternate different values for each member of an array. This lets you do things like alternate formatting to reflect even and odd rows, or mark every 10th element.
excerptFinds a given phrase in a given text and returns the phrase with surrounding context.
highlightFinds a given phrase in a given text and marks all occurrences with
<strong class="highlight" >.markdownWhen used with the BlueCloth plug-in (http://www.deveiate.org/projects/BlueCloth), lets you convert text containing Markdown codes (http://daringfireball.net/projects/markdown/syntax) into HTML.
pluralizeLets you apply Rails’ inflector (the same code that manages singular and plural for ActiveRecord objects) to any text you’d like.
reset_cyclesimple_formattextilizeWhen used with the RedCloth plug-in (http://whytheluckystiff.net/ruby/redcloth/), lets you convert text containing
Textilizecodes (http://www.textism.com/tools/textile) into HTML.textilize_without_paragraphJust like
textilize, but with one fewer surrounding paragraph mark (<p>...</p>).truncateCuts off the end of a string after a specified number of characters and adds a truncate string, usually
....word_wrapWraps text to a specified line width, breaking on whitespace when possible.
The UrlHelper class provides methods for creating links inside of your
Rails application, letting you take advantage of the routing
functionality Rails uses to manage addresses. (And even though the REST
world frequently talks about URIs rather than URLs, URLHelper is the place to go to create
both.)
button_toGenerates a form containing a single button that links to the specified controller. (It’s the same as
url_for, but wraps the result in a button.) You can ask users to confirm their interest after clicking the button.current_page?Returns
trueif the current page has the same URL as the URL created by the options listed.link_toCreates a link (
a) element linking to the specified controller. (It’s the same asurl_for, but wraps the result in an HTML link.)link_to_iflink_to_unlesslink_to_unless_currentLike
link_to, but won’t link if the link is to the current page.mail_tourl_forCreates a URL based on the options provided and the Rails application’s routing table.
If you enjoyed this excerpt, buy a copy of Learning Rails .
