| 21 | | # The template helpers serve to relieve the templates from including the same inline code again and again. It's a |
|---|
| 22 | | # set of standardized methods for working with forms (FormHelper), dates (DateHelper), texts (TextHelper), and |
|---|
| 23 | | # Active Records (ActiveRecordHelper) that's available to all templates by default. |
|---|
| | 21 | # The Rails framework provides a large number of helpers for working with +assets+, +dates+, +forms+, |
|---|
| | 22 | # +numbers+ and <tt>Active Record objects</tt>, to name a few. These helpers are available to all templates |
|---|
| | 23 | # by default. |
|---|
| 25 | | # It's also really easy to make your own helpers and it's much encouraged to keep the template files free |
|---|
| 26 | | # from complicated logic. It's even encouraged to bundle common compositions of methods from other helpers |
|---|
| 27 | | # (often the common helpers) as they're used by the specific application. |
|---|
| | 25 | # In addition to using the standard template helpers provided in the Rails framework, creating custom helpers to |
|---|
| | 26 | # extract complicated logic or reusable functionality is strongly encouraged. By default, the controller will |
|---|
| | 27 | # include a helper whose name matches that of the controller, e.g., <tt>MyController</tt> will automatically |
|---|
| | 28 | # include <tt>MyHelper</tt>. |
|---|
| | 29 | # |
|---|
| | 30 | # Additional helpers can be specified using the +helper+ class method in <tt>ActionController::Base</tt> or any |
|---|
| | 31 | # controller which inherits from it. |
|---|
| 41 | | # Let's hear what the helper has to say: <tt><%= hello_world %></tt> |
|---|
| | 54 | # <% @events.each do |event| -%> |
|---|
| | 55 | # <p> |
|---|
| | 56 | # <% format_time(event.time, :short, "N/A") %> | <%= event.name %> |
|---|
| | 57 | # </p> |
|---|
| | 58 | # <% end -%> |
|---|
| | 59 | # |
|---|
| | 60 | # Finally, assuming we have two event instances, one which has a time and one which does not, |
|---|
| | 61 | # the output might look like this: |
|---|
| | 62 | # |
|---|
| | 63 | # 23 Aug 11:30 | Carolina Railhawks Soccer Match |
|---|
| | 64 | # N/A | Carolina Railhaws Training Workshop |
|---|
| | 65 | # |
|---|
| 52 | | # helper :foo |
|---|
| 53 | | # requires 'foo_helper' and includes FooHelper in the template class. |
|---|
| | 76 | # * <tt>*args</tt>: One or more +Modules+, +Strings+ or +Symbols+, or the special symbol <tt>:all</tt>. |
|---|
| | 77 | # * <tt>&block</tt>: A block defining helper methods. |
|---|
| | 78 | # |
|---|
| | 79 | # ==== Examples |
|---|
| | 80 | # When the argument is a +String+ or +Symbol+, the method will provide the "_helper" suffix, require the file |
|---|
| | 81 | # and include the module in the template class. The second form illustrates how to include custom helpers |
|---|
| | 82 | # when working with namespaced controllers, or other cases where the file containing the helper definition is not |
|---|
| | 83 | # in one of Rails' standard load paths: |
|---|
| | 84 | # helper :foo # => requires 'foo_helper' and includes FooHelper |
|---|
| | 85 | # helper 'resources/foo' # => requires 'resources/foo_helper' and includes Resources::FooHelper |
|---|
| | 94 | # Additionally, the +helper+ class method can receive and evaluate a block, making the methods defined available |
|---|
| | 95 | # to the template. |
|---|
| | 96 | # # One line |
|---|
| | 97 | # helper { def hello() "Hello, world!" end } |
|---|
| | 98 | # # Multi-line |
|---|
| | 99 | # helper do |
|---|
| | 100 | # def foo(bar) |
|---|
| | 101 | # "#{bar} is the very best" |
|---|
| | 102 | # end |
|---|
| | 103 | # end |
|---|
| | 104 | # |
|---|
| | 105 | # Finally, all the above styles can be mixed together, and the helper method can be invokved with a mix of |
|---|
| | 106 | # +symbols+, +strings+, +modules+ and blocks. |
|---|