1.22. Helpers

Helpers or view helpers are modules that are available to use in your views. They provide shortcuts for commonly used code snippits. Here is a list of commonly used helpers and their methods.

1.22.1. ActiveRecordHelper

The ActiveRecordHelper makes it easier to create forms for records kept in instance variables.

The methods are described as follows.

error_message_on
error_message_on(object, method, prepend_text = "", 
append_text = "", css_class = "formError")

Returns a string with a DIV containing all of the error messages for the objects located as instance variables by the names given.

Example

<%= error_message_on "post", "title" %>
# <div class="formError">can't be empty</div>

<%= error_message_on "post", "title", "Title simply ", 
                     " (or it won't work).", "inputError" %> 
# <div class="inputError">Title simply can't be empty 
  (or it won't work).</div>
error_messages_for
error_messages_for(*params) 

Returns a string with a DIV containing all of the error messages for the objects located as instance variables by the names given.

This DIV can be tailored with the following options:

header_tag

Used for the header of the error DIV (default: h2).

id

The id of the error DIV (default: errorExplanation).

class

The class of the error DIV (default: errorExplanation).

object

The object (or array of objects) for which to display errors, if you need to escape the instance variable convention.

object_name

The object name to use in the header, or any text that you prefer. If object_name is not set, the ...

Get Rails Pocket Reference now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.