Rails:如何在 lib 中的单独类中使用助手 [英] Rails: How do I use helpers in a separate class in lib

查看:51
本文介绍了Rails:如何在 lib 中的单独类中使用助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰巧在 lib 文件夹中编写了一个文件,我想在该文件中使用 TextHelper.如何使 Texthelper 可用?

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?

感谢建议,谢谢,

推荐答案

其实并没有那么难.您可以只包含类中的 TextHelper 模块.

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"

这是我在 lib 中定义的一个类,并从 script/console 会话输出以确保一切正常.

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

这篇关于Rails:如何在 lib 中的单独类中使用助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆