2017-05-06 64 views
0

我正在尝试对number_to_currency方法进行一些测试,但无论如何尝试在控制台中调用它,我似乎都很幸运。为什么我无法从Rails控制台调用ActionView方法?

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

这可能有资格作为重复,我不完全确定。另一个问题是范围更广泛,因此有更多方法可以让答案中出现的东西消失,我也相信它可能有一些过时的答案。

+2

可能重复[如何从控制台在Rails中调用控制器/视图方法?](http://stackoverflow.com/questions/151030/how-do-i-call-controller-view-方法 - 从控制台在导轨) –

+0

Theres也view_context {},在该块视图方法是可访问的 –

回答

2

::的ActionView ::助手是NumberHelper模块。无论何时你想使用它,你都应该包含它,并且之后可以使用它的所有方法。所以第一步是包括::的ActionView ::助手模块NumberHelper:

include ActionView::Helpers::NumberHelper 

,然后调用需要的方法:

number_to_currency 12321334543.00 

看看Ruby modules tutorial

编辑:

掌握Rails的佣工自举一个控制台会话也是一种痛苦,其助手可以修复为您服务!你也可以使用它来构建HTML标签,或者ActionView知道的任何现有的Rails助手(签出this blog post)。

helper.number_to_currency 12321334543.00 
1

尝试helper.number_to_currency 12321334543.00

相关问题