RubyOnRails:如何在Rails控制台中使用辅助方法?

时间:2022-11-30 20:46:49

Probably something wrong with my setup:

我的设置可能有问题:

irb(main):001:0> truncate("Once upon a time in a world far far away", :length => 17)
NoMethodError: undefined method `truncate' for main:Object
        from (irb):1
        from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
        from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
        from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

Looks like I cannot use any text helpers (both in irb and rails console).

看起来我不能使用任何文本助手(在irb和rails控制台中)。

What should I check?

我应该检查什么?

2 个解决方案

#1


17  

The Rails Console exposes the helper methods through the helper variable. Therefore, please, use this instead:

Rails控制台通过辅助变量公开辅助方法。因此,请使用此代替:

helper.truncate("Once upon a time in a world far far away", :length => 17)

for more, please read this article on 37signals.com

有关更多信息,请阅读37signals.com上的这篇文章

#2


2  

type following line into your rails console

在rails控制台中键入以下行

include ActionView::Helpers

包括ActionView :: Helpers

now your helpers are accessible during the entire rails console session and you can continue like…

现在您的助手可以在整个rails控制台会话期间访问,您可以继续...

truncate("Once upon a time in a world far far away", :length => 17)

截断(“很久以前的世界”,长度=> 17)

#1


17  

The Rails Console exposes the helper methods through the helper variable. Therefore, please, use this instead:

Rails控制台通过辅助变量公开辅助方法。因此,请使用此代替:

helper.truncate("Once upon a time in a world far far away", :length => 17)

for more, please read this article on 37signals.com

有关更多信息,请阅读37signals.com上的这篇文章

#2


2  

type following line into your rails console

在rails控制台中键入以下行

include ActionView::Helpers

包括ActionView :: Helpers

now your helpers are accessible during the entire rails console session and you can continue like…

现在您的助手可以在整个rails控制台会话期间访问,您可以继续...

truncate("Once upon a time in a world far far away", :length => 17)

截断(“很久以前的世界”,长度=> 17)