Read more

How to use helper methods inside a model

Tobias Kraze
June 24, 2011Software engineer at makandra GmbH

Simple

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

ApplicationController.helpers.my_helper_method

More flexible

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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

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

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.

Posted by Tobias Kraze to makandra dev (2011-06-24 17:38)