What's New in Edge Rails: A Better Way to Access Your Helpers

Posted by ryan
at 2:14 PM on Monday, February 26, 2007



If you’ve any sort of organizational sense to you at all chances are you’ve got quite a few helpers tucked in your app/helpers directory (not the stock ones that go one-to-one with your controllers, but other custom ones you’ve written to handle various other common tasks). If so, your application controller probably also has quite a few helper statements to make them available:

1
2
3
4
5
6
7
class ApplicationController < ActionController::Base

  # Our many helpers that need to be available throughout the app
  [:formatting, :highlighting, :i18n].each { |h| helper h }

  ...
end

We’ve now got a convenient way to bulk include our helpers.

1
2
3
4
5
6
7
class ApplicationController < ActionController::Base

  # Make all helpers in app/helpers/**/ available
  helper :all

  ...
end

Update: Feb. 26th: I previously thought you had to have helper sub-directories to get them properly loaded, but DHH has corrected me. Any and all helpers in your helper directories will get pulled in

And to top things off this is now the default behavior – meaning that all helpers in your helpers directory are automatically included for you. (Hey – you wanted opinionated software)

tags: rubyonrails, rails

Comments

Leave a response

  1. DHHFebruary 26, 2007 @ 03:36 PM

    You don’t need sub-directories. */.rb will match the current directory too. I actually don’t use sub-directories at all. This was mostly added to accomodate people using nested controllers.

  2. Ryan DaigleFebruary 26, 2007 @ 08:20 PM

    DHH: My bad – you guys have kept me too busy recently – had a momentary lapse.

  3. Dr NicFebruary 27, 2007 @ 04:40 AM

    If

    helper :all
    is run by default in the AppController, how is calling
    helper :other
    useful if OtherHelper is already included?

  4. Ryan DaigleFebruary 27, 2007 @ 07:57 AM

    Dr Nic – I don’t think it is. Basically you can do away with the need to call helper anything.

  5. Ryan B.February 27, 2007 @ 01:29 PM

    Since all controllers inherit from ApplicationController does this mean all controller’s views now have access to all helper methods even if they are defined in some other controller’s helper?

  6. Seth Thomas RasmussenMarch 25, 2007 @ 01:26 PM

    Why is this considered more desirable?