Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的局部变量或方法“number”到“u分隔';_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 未定义的局部变量或方法“number”到“u分隔';

Ruby on rails 未定义的局部变量或方法“number”到“u分隔';,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我使用的是rails 4.2.5 在rails API(适用于v4.2.5.2)中,我看到了以下帮助程序: number\u到\u分隔(number,options={}) 使用分隔符(例如12324)将数字格式化为分组的千。您可以在选项哈希中自定义格式 但当我在视图中使用此帮助时,它会抛出一个错误: 未定义的方法用于#` 其他辅助工具,如数字到货币,都可以正常工作。我怎么了?试着打电话给ActiveSupport::NumberHelper ActiveSupport::NumberHelp

我使用的是rails 4.2.5

在rails API(适用于v4.2.5.2)中,我看到了以下帮助程序:

number\u到\u分隔(number,options={})

使用分隔符(例如12324)将数字格式化为分组的千。您可以在选项哈希中自定义格式

但当我在视图中使用此帮助时,它会抛出一个错误:

未定义的方法
用于#`


其他辅助工具,如
数字到货币
,都可以正常工作。我怎么了?

试着打电话给
ActiveSupport::NumberHelper

ActiveSupport::NumberHelper.number_to_delimited(12345678)
 => "12,345,678"
或者你也可以这样做:

include ActiveSupport::NumberHelper
number_to_delimited(12345678)
 => "12,345,678"
更新:

我看到您在上面的评论中说,您使用的是haml代码,您可以这样做:

= ActiveSupport::NumberHelper.number_to_delimited(12345678)


只需在ApplicationHelper中包含ActiveSupport::NumberHelper

module ApplicationHelper
  include ActiveSupport::NumberHelper
end
然后可以直接使用视图中的所有数字辅助对象

<%= number_to_delimited(12345678) %>

number\u to\u delimited
ActiveSupport::NumberHelper
中的一种方法,不能直接在视图中使用

Rails在
ActionView::helpers::NumberHelper
中提供了两个数字助手,它们被委托给
ActiveSupport::NumberHelper
中的方法

# File actionview/lib/action_view/helpers/number_helper.rb, line 244
def number_with_delimiter(number, options = {})
  delegate_number_helper_method(:number_to_delimited, number, options)
end
  • 从数字到货币
  • 数字对人
  • 数字到人的大小
  • 数量对百分比
  • 电话号码
  • 带分隔符的数字\u
  • 带精度的数字
例如,如果要对数字进行分隔,则应使用_delimiter调用
number\u,这将在
ActiveSupport::NumberHelper
中调用
number\u以_delimited

# File actionview/lib/action_view/helpers/number_helper.rb, line 244
def number_with_delimiter(number, options = {})
  delegate_number_helper_method(:number_to_delimited, number, options)
end

希望这是有意义的。干杯。

您在视图中如何使用它?@Pavan我正在使用haml,代码看起来像
=number\u to\u delimited(number)
@Hegwin,我在下面用haml代码和rails控制台回答。希望它能帮助你。谢谢你,但我还是不明白为什么我不能像其他数字助手一样使用它。