0

我正在尝试从祖先 gem 表中填充 jquery jstree。我正在使用 jbuilder 为 jstree 创建 json 输入。

jstree 要求 json 数据以 [ 开头

所以,我正在使用

json.array!(@locations) do |location|

但是,我正在遍历孩子,所以我只需要数组中的第一个位置。我尝试了以下方法,但似乎不起作用:

json.array!(@locations).first(1) do |location|
  json.label location.name
  json.children location.children do |child|
    json.label child.name

更新

这也不起作用:

json.array!(@locations.first) do |location|

UDPATE2

这有效,除了它以 { 开头并且它必须以 [ 开头

这就是我尝试数组的原因。我该如何解决?

(我知道我还需要处理循环逻辑)

json.id @location.id
json.label @location.name
json.children @location.children do |child|
 json.id child.id
 json.label child.name
 json.children child.children do |child2|
   json.id child2.id
   json.label child2.name
   json.children child2.children do |child3|
     json.id child3.id
     json.label child3.name
   end
 end
end

结果是:

{
   id: 1,
   label: "First in Tree"
  - children: [
    - {
        id: 2,
        label: "Child of 1"

...

4

1 回答 1

0

数组中的第一个位置就是

location = json.array!(@locations).first
于 2013-02-14T18:33:44.560 回答