简体   繁体   English

为什么不能从Rails控制台调用ActionView方法?

[英]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. 我正在尝试对number_to_currency方法进行一些测试,但是无论我如何尝试在控制台中调用它,我似乎都不走运。

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. 另一个问题的范围要广得多,因此有很多方法可以使答案中出现的问题得到解决,我也相信它可能会有一些过时的答案。

ActionView::Helpers::NumberHelper is a module. ActionView :: Helpers :: NumberHelper是一个模块。 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: 因此,第一步是包括ActionView :: Helpers :: NumberHelper模块:

include ActionView::Helpers::NumberHelper

and then call the method you want: 然后调用所需的方法:

number_to_currency 12321334543.00

Take a look at Ruby modules tutorial . 看一下Ruby模块教程

Edit: 编辑:

Getting a console session bootstrapped with Rails' helpers is also a pain, which helper can fix for you! 使用Rails的帮助程序引导控制台会话也是很痛苦的,该帮助程序可以为您解决! You could also use it to play with building HTML tags, or any existing Rails helper that ActionView knows about (checkout this blog post ). 您也可以使用它来构建HTML标记或ActionView知道的任何现有Rails助手(请参阅此博客文章 )。

helper.number_to_currency 12321334543.00

尝试helper.number_to_currency 12321334543.00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM