活动模型序列化程序 rails json 在另一个 json 对象中不起作用

active model serializers rails json doesnt work inside another json object

我在 rails 中使用活动模型序列化器,这是我的代码片段

# 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"
},]

但是它只渲染@users

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

我只想删除创建于 json 并更新于 json。 如何解决这个.. 对不起英语不好

I just wanna delete created at and update at from the json.

您可以使用 as_json

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

哪个应该给你

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