繁体   English   中英

使用活动模型序列化程序进行简单json渲染的异常响应

[英]Unusual Response for simple json render with active model serializer

我的Rails 4项目中有以下文件:

lists_controller.rb

class Api::V1::ListsController < Api::V1::ApiController
  before_action :set_list, only: [:show]

  attr_accessor :list

  def show
    respond_with list
  end

  private

  def set_list
    @list = List.where(id: params[:id]).first
    render_list_not_found if @list.nil?
  end

  def render_list_not_found
    render json: { "list" => { message: "List not found" } }, status: 404
  end

end

list_serializer.rb

class ListSerializer < ActiveModel::Serializer
  embed :ids, include: true

  attributes :id, :title, :start_date, :end_date

  has_many :items
end

当我用一个存在列表的ID打API时,我会得到正确的json。 但是,当您传递无效的ID时,您会以为会得到以下信息:

 {
  "list": {
     "message": "List not found"
   }
 }

而是得到这个:

 {
    "lists": [
        [
            "list",
            [
                [
                    "message",
                    "List not found."
                ]
            ]
        ]
    ]
}

知道为什么会这样吗?

似乎即使使用简单的渲染,仍会调用活动模型序列化程序。 我使用的是0.9.1版的gem,但是降级到0.9.0似乎可以解决问题。 很奇怪。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM