【问题标题】:Rendering nested JSON and Object Relationships呈现嵌套的 JSON 和对象关系
【发布时间】:2016-09-16 23:00:03
【问题描述】:

我正在尝试在我的 Rails 应用程序中建立类似于以下 SO 问题中的关系。 Rails Object Relationships and JSON Rendering

我希望我的 JSON 以与所示相同的方式呈现:

[    
    {
      "modelb": {
        "id": "1",
        "modela": [insert the ModelA JSON for ID's 1, 2 and 3]
      }
    {
      "modelb": {
        "id": "2",
        "modela": [insert the ModelA JSON for ID's 3, 4 and 5]
      }
    }
]

我已经有一个控制器来创建模型 A 所需的 JSON,如下所示,但我不知道如何制作 ModelB 模型和控制器以便将该信息嵌套到另一组 JSON 中,或者如何指定哪个ModelA 对象进入 ModelB 的哪个对象中。

class JsonsController < ApplicationController
  before_action :set_json, only: [:show, :update, :destroy]

  # GET /jsons
  # GET /jsons.json
  def index
    @jsons = Json.all

    render json: @jsons
  end

  # GET /jsons/1
  # GET /jsons/1.json
  def show
    render json: @json
  end

  # POST /jsons
  # POST /jsons.json
  def create
    @json = Json.new(json_params)

    if @json.save
      render json: @json, status: :created, location: @json
    else
      render json: @json.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /jsons/1
  # PATCH/PUT /jsons/1.json
  def update
    @json = Json.find(params[:id])

    if @json.update(json_params)
      head :no_content
    else
      render json: @json.errors, status: :unprocessable_entity
    end
  end

  # DELETE /jsons/1
  # DELETE /jsons/1.json
  def destroy
    @json.destroy

    head :no_content
  end

  private

    def set_json
      @json = Json.find(params[:id])
    end

    def json_params
      params.require(:json).permit(:text, :parent, :id)
    end
end

任何资源或帮助将不胜感激。

【问题讨论】:

  • 我认为您的处理方式不正确。为什么你的模型命名为 Json?它应该是 ModelB 然后你可以做 @models = ModelB.find(params[:id]) 然后 @models.to_json(:include => :modela) 发布你的模型代码会有所帮助。如果您想聊聊,请随时留言
  • 我已将其更改为modelB,我将把您指定的代码放在哪里?我的模型代码只是一个 has_many ,belongs_to 关系,如引用的链接所示。谢谢

标签: javascript ruby-on-rails ruby json


【解决方案1】:

正如@alexbezek 已经说过你需要在控制器中获得ModelB,而不是Json

Rails 有一个很棒的 gem jbuilder 可以渲染一个 json 对象。您只需为每个 json 操作创建视图。

示例如下:

# Controller
# ...  
def index
  @models_b = ModelB.all
end
...

然后查看

#views/model_b/index.jbuilder

json.array! @models_b do |item|
  json.id item.id,
  json.modela: item.models_a
end

或者作为一种可能的方法,您可以在要显示为 json 的每个模型中定义方法 as_json。这是一种将模型渲染为 json 对象的“手动非惰性”方式。

例如:

# ModelB
def as_json
  jsn = {
    id: id,
    name: name,
    any_other_attr_you_need: any_other_attr_you_need
  }
jsn[:modela] = models_a.as_json if models_a.any?
end

#ModelA

def as_json
  jsn = {
    id: id,
    name: name,
    etc: etc
  }
  jsn
end


# Controller
# ...  
def index
  @models_a = ModelA.all
  render json: @models_a.as_json
end
...

【讨论】:

  • 感谢您的帮助,但我正在关注您的示例并对其进行测试,但仍然不明白。您的示例中的 modelb 如何将 json 从 modelA 获取到嵌套?
  • 所以我开始让 as_json 解决方案工作,但在 ModelB 中我得到一个 NameError ,说 models_a 是未定义的局部变量/方法。有没有办法解决这个问题?
  • 更新:能够使用 def as_json 方法嵌套 JSON,但它返回嵌套在每个 model_b 对象中的每个 model_a 对象,而不是能够将它们关联到 model_a 对象属于 model_b 对象的位置,这has_many
  • @Kevin,你能提供你的模型代码吗?没有它很难找出原因。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 2023-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多