【发布时间】:2014-12-07 04:43:21
【问题描述】:
我的 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."
]
]
]
]
}
知道为什么会这样吗?
【问题讨论】:
标签: ruby-on-rails json ruby-on-rails-4 active-model-serializers