2017-05-30 32 views
-1

用我的軌道主動型串行器(IAM),這是我的片斷主動型串行導軌內的另一個JSON對象JSON不工作

# GET /users 
    def index 
    @users = User.all 
    render json: {data: @users, status: httpstatus[:getSuccess]} 
    # render json: {data: @users, status: httpstatus[:getSuccess]} 
    end 

但其表示像這樣的郵遞員

"data": [ 
{ 
    "id": 38, 
    "name": "tyasa", 
    "age": 21, 
    "created_at": "2017-05-30T06:23:03.504Z", 
    "updated_at": "2017-05-30T06:23:03.504Z" 
}, 
{ 
    "id": 39, 
    "name": "wahyu", 
    "age": 21, 
    "created_at": "2017-05-30T06:23:37.359Z", 
    "updated_at": "2017-05-30T06:23:37.359Z" 
},] 

但其工作時,只是呈現@用戶

# GET /users 
    def index 
    @users = User.all 
    render json: {data: @users, status: httpstatus[:getSuccess]} 
    # render json: @users 
    end 

我只是想刪除創建和從json更新。 如何解決這個.. 索裏的英語不好

+0

請遵循https://stackoverflow.com/questions/15397537/rails-model-to-哈希排除的屬性 –

回答

0

我只是想在刪除創建並從JSON更新。

您可以使用as_json

render json: {data: @users.as_json(:except => [:created_at, :updated_at]), status: httpstatus[:getSuccess]} 

except選項,它應該給你

"data": [ 
{ 
    "id": 38, 
    "name": "tyasa", 
    "age": 21 
}, 
{ 
    "id": 39, 
    "name": "wahyu", 
    "age": 21 
},] 
相關問題