Jeremy
Jeremy

Reputation: 675

Why can't I call an ActionView method from the Rails Console?

I'm trying to do a little testing of the number_to_currency method, but no matter how I try and call it in the console I'm out of luck it seems.

ActionView::Helpers::NumberHelper::number_to_currency 12321334543.00
# => NoMethodError: undefined method `number_to_currency' for ActionView::Helpers::NumberHelper:Module

This may qualify as a duplicate, I am not entirely sure. The other question is a lot broader in scope, and therefore has a lot more ways of getting things gone presented in the answers, and I also believe it may have some outdated answers.

Upvotes: 4

Views: 1352

Answers (2)

Aref Aslani
Aref Aslani

Reputation: 1636

ActionView::Helpers::NumberHelper is a module. Whenever you want to use it you should include it and all it's method could be used after that. So the first step is including ActionView::Helpers::NumberHelper module:

include ActionView::Helpers::NumberHelper

and then call the method you want:

number_to_currency 12321334543.00

Take a look at Ruby modules tutorial.

Edit:

Getting a console session bootstrapped with Rails’ helpers is also a pain, which helper can fix for you! You could also use it to play with building HTML tags, or any existing Rails helper that ActionView knows about (checkout this blog post).

helper.number_to_currency 12321334543.00

Upvotes: 4

Ilya Lavrov
Ilya Lavrov

Reputation: 2860

try helper.number_to_currency 12321334543.00

Upvotes: 2

Related Questions