2016-08-23 47 views
2

我使用的是內置的API使用Rails 5.Rails的API:添加對象的數組JSON回報

我學習寫使用Rails的API,我試圖找出如何添加一個屬性,我的json返回的是一個對象數組。

我有一個用戶和帖子的模型。

我想要做的是返回與用戶關聯的所有帖子。

我所做的是在posts_controller.rb我已經得到了獲取用戶ID形成URL和返回的JSON,看起來像一個方法:

[{"id":1,"content":"My first post!","user":{"id":1,"firstname":"Jody","lastname":"White","email":"[email protected]","fullname_firstname_first":"Jody White"}},{"id":2,"content":"Rails School is awesome!","user":{"id":1,"firstname":"Jody","lastname":"White","email":"[email protected]","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 
+0

您可以發佈您的模型,展示涉及的關聯嗎?我們可能不需要看到完整的模型。 – jaydel

+0

@jaydel更新了模型的帖子。 –

回答

2

假設你定義的這個結構:

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 
+0

在我的show方法中,我將'render json:@ user'更改爲'render json:@ user.posts',我得到的格式與原始文章相同。如果它很重要,我正在使用主動模型串行器。 –

+0

我正在使用Rails 5,它看起來像respond_to已被棄用?我發現一篇文章說如果我想使用respond_to,我需要向application_controller添加'include ActionController :: MimeResponds'。rb但是當我這樣做時,我得到''狀態「:406,」error「:」Not Acceptable「,」exception「:」#\ u003cActionController :: UnknownFormat:ActionController :: UnknownFormat \ u003e「' –

+0

@ Christian-已經有我的控制器和方法現在看起來像你的,但我沒有得到包括在回覆中的帖子。如果有幫助,請參閱我編輯的原始文章以查看模型。 –