helper(*args, &block) public

Includes the given modules in the template class.

Modules can be specified in different ways. All of the following calls include FooHelper:

# Module, recommended.
helper FooHelper

# String/symbol without the "helper" suffix, camel or snake case.
helper "Foo"
helper :Foo
helper "foo"
helper :foo

The last two assume that "foo".camelize returns “Foo”.

When strings or symbols are passed, the method finds the actual module object using String#constantize. Therefore, if the module has not been yet loaded, it has to be autoloadable, which is normally the case.

Namespaces are supported. The following calls include +Foo::BarHelper+:

# Module, recommended.
helper Foo::BarHelper

# String/symbol without the "helper" suffix, camel or snake case.
helper "Foo::Bar"
helper :"Foo::Bar"
helper "foo/bar"
helper :"foo/bar"

The last two assume that "foo/bar".camelize returns “Foo::Bar”.

The method accepts a block too. If present, the block is evaluated in the context of the controller helper module. This simple call makes the wadus method available in templates of the enclosing controller:

helper do
  def wadus
    "wadus"
  end
end

Furthermore, all the above styles can be mixed together:

helper FooHelper, "woo", "bar/baz" do
  def wadus
    "wadus"
  end
end
Show source
Register or log in to add new notes.