Search code examples
ruby-on-railslayoutscopehelpercontrollers

Rails helper for views and controllers?


I know there are ways to make helpers available to controllers, but that this is generally bad form. How should the following be re-worked to avoid this?

I have a Contacts helper called "fullname" that combines a contact's first and last names.

A layout partial called subheader is rendered in the application layout, and contains this code:

<section id="subheader"><%= @subheader %></section>

The controllers set the value of @subheader.

The issue is that I often want "fullname" in @subheader. But this means accessing the helper from the controller. Should this fullname method reside somewhere else?


Solution

  • With something like fullname I usually just define a method on the model itself.

    If the fullname method did some type of HTML formatting then I would keep those parts inside a helper.