timpone
timpone

Reputation: 19929

How to use a method in a model class that is defined in a helper module

I am just trying to access the simple_format helper

class Note < ActiveRecord::Base
  include SimpleHelper

 def body_symbols_output_html
    arc_simple_format(body_symbols_output)
  end

and in lib/simple_helper.rb

module SimpleHelper
  def arc_simple_format txt
    simple_format txt
  end
end

and get

NoMethodError (undefined method `simple_format' for #<Note:0x007f7fbb913088>):
  lib/simple_helper.rb:4:in `arc_simple_format'

How would I access simple_format? I know accessing view level helpers is a bad idea but this is just feeding an API.

Upvotes: 3

Views: 1848

Answers (1)

apneadiving
apneadiving

Reputation: 115511

You should simply include the proper module:

include ActionView::Helpers::TextHelper

what I'd suggest though is to avoid to spoil your class with all those methods, so you could create a Helper class in your class:

class Note
  delegate :simple_format, to: :helper

  def helper
    Helper.instance
  end

  private

  class Helper
    include Singleton
    include ::ActionView::Helpers::NumberHelper
  end
end

Upvotes: 6

Related Questions