Recipe 5.1. Simplifying Templates with View Helpers


Problem

View templates are supposed to be for presentation: they should contain HTML and minimal additional logic to display data from the model. You want to keep your view templates clear of program logic that might get in the way of the presentation.

Solution

Define methods in a helper module named after the controller whose views will use those methods. In this case, create helper methods named display_new_hires and last_updated within a module named IntranetHelper (named after the Intranet controller).

app/helpers/intranet_helper.rb:

module IntranetHelper   def display_new_hires     hires = NewHire.find :all, :order => 'start_date desc', :limit => 3     items = hires.collect do |h|       content_tag("li",         "<strong>#{h.first_name} #{h.last_name}</strong>" +         " - #{h.position} (<i>#{h.start_date}</i>)")     end     return content_tag("b", "New Hires:"), content_tag("ul",items)   end   def last_updated(user)     %{<hr /><br /><i>Page last update on #{Time.now} by #{user}</i>}   end end

Within the index view of the Intranet controller you can call your helper methods just like any other system method.

app/views/intranet/index.rhtml:

<h2>Intranet Home</h2> <p>Pick the Hat to Fit the Head -- October 2004. Larry Wall once said, Information wants to be valuable, and the form in which information is presented contributes to that value. At O'Reilly Media, we offer a variety of ways to get your technical information. Tim O'Reilly talks about it in his quarterly letter for the O'Reilly Catalog.,</p> <%= display_new_hires %> <%= last_updated("Goli") %>

Discussion

Helper methods are implemented in Rails as modules. When Rails generates a controller, it also creates a helper module named after that controller in the app/helpers directory. By default, methods defined in this module are available in the view of the corresponding controller. Figure 5-1 shows the output of the view using the display_new_hires and last_updated helper methods.

Figure 5-1. The results of the display_new_hires view helper


If you want to share helper methods with other controllers, you have to add explicit helper declarations in your controller. For example, if you want the methods in the IntranetHelper module to be available to the views of your Store controller, pass :intranet to the Store controller's helper method:

class StoreController < ApplicationController   helper :intranet end

Now it will look for a file called helpers/intranet_helper.rb and include its methods as helpers.

You can also make controller methods available to views as helper methods by passing the controller method name to the helper_method method. For example, this StoreController allows you to call <%= get_time %> in your views to display the current time.

class StoreController < ApplicationController   helper_method :get_time   def get_time     return Time.now   end end

See Also

  • Section 5.11"

  • Section 5.12"




Rails Cookbook
Rails Cookbook (Cookbooks (OReilly))
ISBN: 0596527314
EAN: 2147483647
Year: 2007
Pages: 250
Authors: Rob Orsini

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net