RubyOnRails: How do I use helper methods in Rails Console?

5.4k views Asked by At

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).

What should I check?

2

There are 2 answers

0
Stoic On BEST ANSWER

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

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

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

0
netznarkose On

type following line into your rails console

include ActionView::Helpers

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

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