我碰巧在 lib 文件夹中创建了一个文件,我想在该文件中使用 TextHelper。如何使 Texthelper 可用?
建议赞赏,
谢谢,
最佳答案
其实一点也不难。您可以只包含 TextHelper
您类(class)的模块。
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
输出 session 以确保一切顺利。
关于ruby-on-rails - rails : How do I use helpers in a separate class in lib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/896134/