【问题标题】:How to encode 2 objects in the same JSON object usin JBuilder Rails如何使用 JBuilder Rails 在同一个 JSON 对象中编码 2 个对象
【发布时间】:2013-12-02 07:41:34
【问题描述】:

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

我该怎么做?

【问题讨论】:

    标签: ruby-on-rails jbuilder


    【解决方案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 objects can be directly nested inside each other. Useful for composing objects.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2011-10-19
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      相关资源
      最近更新 更多