【问题标题】:active model serializers rails json doesnt work inside another json object活动模型序列化器 rails json 在另一个 json 对象中不起作用
【发布时间】:2017-05-30 07:25:03
【问题描述】:

我在我的 Rails 中使用活动模型序列化程序,这是我的 sn-p

# 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 中删除创建于并更新于。 这个怎么解决。。 英语不好

【问题讨论】:

标签: ruby-on-rails json ruby active-model-serializers


【解决方案1】:

我只想从 json 中删除创建于并更新于。

你可以使用as_jsonexcept选项

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

【讨论】:

    猜你喜欢
    • 2015-08-23
    • 1970-01-01
    • 2023-04-06
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    相关资源
    最近更新 更多