【问题标题】:Rails API: Adding array of objects to json returnRails API:将对象数组添加到json返回
【发布时间】:2016-08-23 13:30:37
【问题描述】:

我正在使用 Rails 5 的内置 API。

我正在学习使用 Rails 编写 API,我正在尝试弄清楚如何向我的 json 返回添加一个属性,该属性是一个对象数组。

我有一个用户和帖子的模型。

我想做的是返回与用户相关的所有帖子。

我所做的是在 posts_controller.rb 中我有一个方法可以从 URL 获取用户 ID 并返回如下所示的 json:

[{"id":1,"content":"My first post!","user":{"id":1,"firstname":"Jody","lastname":"White","email":"t@t.com","fullname_firstname_first":"Jody White"}},{"id":2,"content":"Rails School is awesome!","user":{"id":1,"firstname":"Jody","lastname":"White","email":"t@t.com","fullname_firstname_first":"Jody White"}}]

但我想要返回的是这样的:

{
    firstname: "Jody",
    lastname: "White",
    email: "whatever",
    posts: [{
        "id":1,"content":"My first post!"
        },
        {
            "id":2,"content":"Rails School is awesome!"
        }
    ]
}

我如何才能或者我可以让数据以这样的方式返回?

user.rb 模型

class User < ApplicationRecord
  attr_accessor :fullname_firstname_first

  has_many :posts

  def fullname_firstname_first
    fullname_firstname_first = firstname + " " + lastname
  end
end

post.rb 模型

class Post < ApplicationRecord
  belongs_to :user
end

users_controller.rb

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :update, :destroy]

  # GET /users
  def index
    @users = User.all
    render json: @users, include: :posts
  end

  # GET /users/1
  def show
    @user = User.find(params[:id])
    render json: @user, include: :posts
  end
end

【问题讨论】:

  • 你能发布你的模型,展示所涉及的关联吗?我们可能不需要看到完整的模型。
  • @jaydel 用模型更新了帖子。

标签: ruby-on-rails ruby-on-rails-5 rails-api


【解决方案1】:

假设你定义了这个结构:

class Post < ApplicationRecord
  belongs_to :user
end

class User < ApplicationRecord
  has_many :posts
end

您可以使用 Active Record 序列化方法to_json 实现您想要的结构。使用将在控制器中的 respond_to 块中。

format.json { render json: @users, include: :posts }

所以你的UsersController 看起来像这样:

class UsersController < ApplicationController
  def index
    # all users with all their posts 
    @users = User.all
    respond_to do |format|
      format.json { render json: @users, include: :posts }
    end
  end

  def show
    # single user and their posts
    @user = User.find(params[:id])
    respond_to do |format|
      format.json { render json: @user, include: :posts }
    end
  end
end

更新

除了使用repond_to 块,您还可以使用:

render json: @users, include: :posts

您的控制器将如下所示:

class UsersController < ApplicationController
  def index
    # all users with all their posts 
    @users = User.all
    render json: @users, include: :posts
  end

  def show
    # single user and their posts
    @user = User.find(params[:id])
    render json: @user, include: :posts
  end
end

【讨论】:

  • 在我的显示方法中,我将render json: @user 更改为render json: @user.posts,我得到的格式与我原来的帖子相同。如果重要的话,我正在使用 Active Model Serializer。
  • 我使用的是 Rails 5,看起来 respond_to 已被弃用?我发现一个帖子说如果我想使用 respond_to 我需要将 include ActionController::MimeResponds 添加到 application_controller.rb 但是当我这样做时我得到 "status":406,"error":"Not Acceptable","exception":"#\u003cActionController::UnknownFormat: ActionController::UnknownFormat\u003e"
  • @Christian-G 我的控制器和方法现在看起来像你的,但我没有收到回复中包含的帖子。如果有帮助,请参阅我为我的模型编辑的原始帖子。
  • 你的控制器现在是什么样子的?
  • @Christian-G 我原来的帖子已经更新了部分 users_controller.rb 文件
猜你喜欢
  • 2018-07-20
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 2022-01-14
相关资源
最近更新 更多