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

2

8

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?

Honna answered 17/10, 2012 at 20:28 Comment(0)
T
26

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

Troth answered 17/10, 2012 at 20:31 Comment(0)
I
4

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)

Injudicious answered 9/7, 2015 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.