无法将字符串转换为整数"String can't be coerced into Fixnum"



我正在使用祖源宝石和ActiveAdmin。我需要使用depth方法缩进选择选项。我相信depth是一个字符串,所以我想把它转换成int,这样我就可以乘以一个空格。像这样:

    form do |f|

    f.inputs "Category" do
      f.input :ancestry, :as => :select, :collection => ItemsCategory.all.map {|u| [u.depth.to_i * "" + u.name  , u.id]}
      f.input :name
    end
    f.actions
    f.semantic_errors
  end

我假设u.depthFixnum

问题是u.depth * " "。你不能用Fixnum乘以String,这是没有意义的。不过,反过来也可以:

3 * "a"
# => TypeError: String can't be coerced into Fixnum
"a" * 3
# => "aaa"

如果,如你所说:

我相信depth是一个字符串所以我想把它转换成int

然后你可以只做u.depth.to_i(将返回0,如果字符串不是一个数字)。

相关内容

最新更新