Using helpers in model: how do I include helper dependencies? Using helpers in model: how do I include helper dependencies? ruby-on-rails ruby-on-rails

Using helpers in model: how do I include helper dependencies?


Just change the first line as follows :

include ActionView::Helpers

that will make it works.

UPDATE: For Rails 3 use:

ActionController::Base.helpers.sanitize(str)

Credit goes to lornc's answer


This gives you just the helper method without the side effects of loading every ActionView::Helpers method into your model:

ActionController::Base.helpers.sanitize(str)


This works better for me:

Simple:

ApplicationController.helpers.my_helper_method

Advance:

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  endend

Source: http://makandracards.com/makandra/1307-how-to-use-helper-methods-inside-a-model