Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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 使用jBuilder创建自定义数组_Ruby On Rails_Ruby_Jbuilder - Fatal编程技术网

Ruby on rails 使用jBuilder创建自定义数组

Ruby on rails 使用jBuilder创建自定义数组,ruby-on-rails,ruby,jbuilder,Ruby On Rails,Ruby,Jbuilder,我正试图使用jBuilder构建一个自定义数组,它是我的javascript函数所需的结构。在这种情况下,它们的数组中只有一个对象。我知道数组

我正试图使用jBuilder构建一个自定义数组,它是我的javascript函数所需的结构。在这种情况下,它们的数组中只有一个对象。我知道
数组
object = {
  customArray : [{ someKey : 'somevalue' }]
}

不确定这是否是您所要求的,但您可以这样做:

x = { :someKey => 'somevalue' }
Jbuilder.encode do |json|
  json.customArray Array.wrap(x)
end
Array#wrap
ActiveSupport
提供,并将项目包装在数组中(除非它是数组)。它也不会像其他方法(例如,
Array(x)
x.to_a
[*x]
)那样破坏您的
散列