1

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}]}

次に、food_types を詳細に移動する必要があります。

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

どうすればいいですか?

4

1 に答える 1

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

または、Jbuilder オブジェクトを相互に直接ネストできます。オブジェクトの構成に役立ちます。

于 2014-01-23T14:26:20.967 に答える