2009-05-22 84 views

回答

6

其实这并不难的。您可以只包含课程中的TextHelper模块。

class MyLib 
    include ActionView::Helpers::TextHelper 

    def five_things(x) 
    pluralize 5, x 
    end 
end 

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

,从从script/console会议上,我在lib定义一个类,输出的,以确保它所有玩家不错。

+2

奇怪的是这似乎并没有工作,如果它是一个类的方法,如`高清自我.five_things(X)`。任何想法为什么? – 2011-06-09 23:24:34

0

对于那些似乎谁self方法不继承从助手的功能,这将工作:

class MyLib 

    class << self 

    include Path::To::YourHelper 

    def test_func(x) 
     method_in_helper 5, x 
    end 

    end 

end