Rails:如何在lib中的单独类中使用帮助程序

我碰巧在lib文件夹中创建了一个文件,我想在该文件中使用TextHelper。 如何使Texthelper可用?

建议表示赞赏,谢谢,

实际上并不是那么难。 您可以在类中包含TextHelper模块。

 class MyLib include ActionView::Helpers::TextHelper def five_things(x) pluralize 5, x end end >> MyLib.new.five_things "dog" => "5 dogs" 

这是我在lib定义的类,并从script/console会话输出,以确保它们都很好。

对于那些self方法似乎没有从helperinheritance函数的人,这将起作用:

 class MyLib class << self include Path::To::YourHelper def test_func(x) method_in_helper 5, x end end end