Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How do I use helpers in a separate class in lib

I'm happened to write create one file in lib folder and I want to use TextHelper in that file. How can I make Texthelper available?

Suggestions appreciated, Thanks,

like image 372
Sophy Avatar asked May 22 '09 02:05

Sophy


1 Answers

Actually it's not that hard at all. You can just include the TextHelper module from your class.

class MyLib
  include ActionView::Helpers::TextHelper

  def five_things(x)
    pluralize 5, x
  end
end

>> MyLib.new.five_things "dog"
=> "5 dogs"

That's from a class I defined in lib, and output from a script/console session to make sure it all plays nice.

like image 60
Ian Terrell Avatar answered Oct 08 '22 02:10

Ian Terrell