簡體   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