Posted about 11 years ago. Visible to the public.

How to use helper methods inside a model

Simple

If you want to use a helper_method my_helper_method inside a model, you can write

Copy
ApplicationController.helpers.my_helper_method

More flexible

If you need a bit more flexibility, for example if you also need to override some methods, you can do this:

Copy
class HelperProxy < ActionView::Base include ApplicationController.master_helper_module def current_user #let helpers act like we're a guest nil end def self.instance @instance ||= new end end

and then use with

Copy
HelperProxy.instance.my_helper_method

Bare metal

If you have strong nerves, you can also try to include the ApplicationController.master_helper_module directly into your model.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for unsupported versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2).

Owner of this card:

Avatar
Tobias Kraze
Last edit:
over 7 years ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Tobias Kraze to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more