我如何需要NumberHelper并使它正常工作? [英] How do I require NumberHelper and make it work?
问题描述
我正在尝试编写一个简单的Sinatra内容,但是我需要Action Pack中的ActionView :: Helpers :: NumberHelper. http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html
问题是,如何安装和使用它?
irb(main):001:0> require 'action_view/helpers/number_helper'
irb(main):002:0> number_with_precision(1)
NoMethodError: undefined method `number_with_precision' for main:Object
irb(main):004:0> ActionView::Helpers::NumberHelper.number_with_precision(1)
NoMethodError: undefined method `number_with_precision' for ActionView::Helpers::NumberHelper:Module
为什么这个简单的步骤不起作用?
此外,如果我需要所有废话:
irb(main):001:0> require 'action_pack'
irb(main):004:0> require 'action_view'
irb(main):005:0> include ActionView::Helpers::NumberHelper
irb(main):006:0> number_to_phone(12345)
NoMethodError: undefined method `starts_with?' for "12345":String
从所有这些方面如何理解?为什么该模块不起作用?为什么不要求什么呢?需要什么starts_with在哪里?
Google对这些问题完全保持沉默.
UPD:现在我得到了
number_with_precision(1, :locale => 'fr')
TypeError: wrong argument type nil (expected Fixnum)
在我看来,我的NumberHelper坏了.这不是好行为.
因此,经过一些研究,我在Rails的主分支上找到了以下pull请求
https://github.com/rails/rails/pull/6315
它的主要目的是将ActionView::Helpers::NumberHelper
从ActionView
移到ActiveSupport
我还看到了一些已解决的问题,旨在解决一些问题,这些问题允许独立包含NumberHelper
.这意味着需要修复等.我没有找到number_to_phone
的公开问题,但问题根源在于ActiveSupport
向String
类添加了别名starts_with?
的事实.我不确定他们是否在那里发现了该错误.
在任何情况下,对于ActionView
版本3.2.13
,您都可以执行以下操作
require 'action_view'
include ActionView::Helpers::NumberHelper
number_with_precision 3.1
#=> "3.100"
至于number_to_phone
,仍然会与当前版本不符.我目前正在制作PR,以解决该问题.
编辑
关于语言环境问题,看来,如果您指定一个语言环境,则需要在I18n
中设置正确的选项才能使其起作用.如果不提供语言环境,则默认值将类似于此{:separator=>".", :delimiter=>"", :precision=>3, :significant=>false, :strip_insignificant_zeros=>false}
,否则,哈希将为空,并会引起问题.我似乎在Rails上找不到关于它的任何问题.
同样,此问题已在主 https://github.com/carlosantoniodasilva/rails/commit/f6b71499e967e03c65d
更新
您现在可以使用ActiveSupport
来使用这些帮助程序
http://api.rubyonrails.org/classes/ActiveSupport/NumberHelper.html >
I'm trying to write a simple Sinatra thingy but I need ActionView::Helpers::NumberHelper from action pack. http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html
The question is, how do I install and use it?
irb(main):001:0> require 'action_view/helpers/number_helper'
irb(main):002:0> number_with_precision(1)
NoMethodError: undefined method `number_with_precision' for main:Object
irb(main):004:0> ActionView::Helpers::NumberHelper.number_with_precision(1)
NoMethodError: undefined method `number_with_precision' for ActionView::Helpers::NumberHelper:Module
Why doesn't this simple step work?
Moreover, if I require all the crap:
irb(main):001:0> require 'action_pack'
irb(main):004:0> require 'action_view'
irb(main):005:0> include ActionView::Helpers::NumberHelper
irb(main):006:0> number_to_phone(12345)
NoMethodError: undefined method `starts_with?' for "12345":String
How to make sense from all of this? Why does not this module work? Why doesn't it require whatever it needs? What does it need? Where is starts_with?
Google is utterly silent on those questions.
UPD: And now I get the following
number_with_precision(1, :locale => 'fr')
TypeError: wrong argument type nil (expected Fixnum)
It seems to me that my NumberHelper is broken. This isn't a good behavior.
So, after doing a bit of research, I found the following pull request on the master branch of Rails
https://github.com/rails/rails/pull/6315
It pretty much aims to move ActionView::Helpers::NumberHelper
from ActionView
to ActiveSupport
I also saw a few closed issues that aimed to fix a few problems with allowing inclusion of NumberHelper
as standalone. This means require fixes and such. I didn't find an open issue with the number_to_phone
but the problem roots at the fact that ActiveSupport
adds an alias starts_with?
to the String
class. I'm not sure if they have caught that bug there yet or not.
In any case, with ActionView
version 3.2.13
you can do the following
require 'action_view'
include ActionView::Helpers::NumberHelper
number_with_precision 3.1
#=> "3.100"
As for the number_to_phone
, that will still break with the current version. I'm making a PR to fix that issue at this moment.
EDIT
As for the locale issue, it seems that if you specify a local you need to have set the right options in the I18n
for it to work. If you don't provide a locale the defaults will look like this {:separator=>".", :delimiter=>"", :precision=>3, :significant=>false, :strip_insignificant_zeros=>false}
, otherwise, the hash will be empty and it will cause issues. I can't seem to find any issues about it on Rails though.
Again, this was fixed on a PR on master https://github.com/carlosantoniodasilva/rails/commit/f6b71499e967e03c65d53cc890585f42f3b8aaa2
UPDATE
You can use ActiveSupport
now to use these helpers
http://api.rubyonrails.org/classes/ActiveSupport/NumberHelper.html
这篇关于我如何需要NumberHelper并使它正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!