Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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 Rails在同一JSON对象中编码2个对象_Ruby On Rails_Jbuilder - Fatal编程技术网

Ruby on rails 如何使用JBuilder Rails在同一JSON对象中编码2个对象

Ruby on rails 如何使用JBuilder Rails在同一JSON对象中编码2个对象,ruby-on-rails,jbuilder,Ruby On Rails,Jbuilder,使用JBuilder有以下代码: json.details place.details if (place.type == 'restaurant') json.food_types place.details.food_types, :id end JSON响应: details":{"average_check":100,"id":12},"food_types":[{"id":1}]} 现在我需要将食物类型转移到细节: details":{"average_c

使用JBuilder有以下代码:

  json.details   place.details

  if (place.type == 'restaurant')
    json.food_types place.details.food_types, :id
  end
JSON响应:

details":{"average_check":100,"id":12},"food_types":[{"id":1}]}
现在我需要将食物类型转移到细节:

details":{"average_check":100,"id":12", food_types":[{"id":1}]}}

我怎么做呢?

你可以这样建造它

json.details do 
    json.(place.details, :average_check, :id)
    if (place.type == 'restaurant')
       json.food_types place.details.food_types, :id
    end
  end
end

或者你可以这样建造它

json.details do 
    json.(place.details, :average_check, :id)
    if (place.type == 'restaurant')
       json.food_types place.details.food_types, :id
    end
  end
end