Rails 活动模型序列化程序 returns 数组而不是 json
Rails Active model serializer returns array instead of json
我正在使用 Rails 创建包含基本待办事项信息的 API:名称、列表和项目。我希望它是 return json 格式,看起来像这样:
{
"data": [
{
"type": "posts",
"id": "1",
"attributes": {
"title": "JSON API is awesome!",
"body": "You should be using JSON API",
"created": "2015-05-22T14:56:29.000Z",
"updated": "2015-05-22T14:56:28.000Z"
}
}
],
"links": {
"href": "http://example.com/api/posts",
"meta": {
"count": 10
}
}
}
^来自 Active Serializer's Github 的代码。
当我查看本地主机 http://localhost:3000/api/users/ 时,它显示
[{"id":1,"username":"Iggy1","items":[{"id":1,"list_id":1,"name":"Wash dishes","completed":true},{"id":7,"list_id":1,"name":"Finish this assignment","completed":false}],"lists":[{"id":1,"name":"Important!","user_id":1,"permission":"private"},...
它是 return 哈希数组。我确定我在设置序列化程序时错过了一个重要步骤。如何将我的哈希数组重新格式化为 JSON API 格式?
我已经阅读了 getting started guide, rendering, and JSON API 部分,但我仍然无法理解。我可能忽略了它。
我的一些代码:
app/serializers/user_serializer.rb
class UserSerializer < ActiveModel::Serializer
attributes :id, :username#, :email
has_many :items, through: :lists
has_many :lists
end
app/controllers/api/users_controller.rb
def index
@users = User.all
render json: @users, each_serializer: UserSerializer
end
路线
Rails.application.routes.draw做
namespace :api, defaults: { format: :json } do
resources :users do
resources :lists
end
end
end
让我知道是否可以更好地阐明它。谢谢!!
(来自评论的回答)
要使用 JSON API 适配器,您需要声明您要使用它。
ActiveModelSerializers.config.adapter = ActiveModelSerializers::Adapter::JsonApi
根据 AMS readme。
ActiveModelSerializers.config.adapter = :json_api # Default: `:attributes`
但是,这并没有解决我的问题。这是由于哈希被嵌套,如
中所讨论
我正在使用 Rails 创建包含基本待办事项信息的 API:名称、列表和项目。我希望它是 return json 格式,看起来像这样:
{
"data": [
{
"type": "posts",
"id": "1",
"attributes": {
"title": "JSON API is awesome!",
"body": "You should be using JSON API",
"created": "2015-05-22T14:56:29.000Z",
"updated": "2015-05-22T14:56:28.000Z"
}
}
],
"links": {
"href": "http://example.com/api/posts",
"meta": {
"count": 10
}
}
}
^来自 Active Serializer's Github 的代码。
当我查看本地主机 http://localhost:3000/api/users/ 时,它显示
[{"id":1,"username":"Iggy1","items":[{"id":1,"list_id":1,"name":"Wash dishes","completed":true},{"id":7,"list_id":1,"name":"Finish this assignment","completed":false}],"lists":[{"id":1,"name":"Important!","user_id":1,"permission":"private"},...
它是 return 哈希数组。我确定我在设置序列化程序时错过了一个重要步骤。如何将我的哈希数组重新格式化为 JSON API 格式?
我已经阅读了 getting started guide, rendering, and JSON API 部分,但我仍然无法理解。我可能忽略了它。
我的一些代码:
app/serializers/user_serializer.rb
class UserSerializer < ActiveModel::Serializer
attributes :id, :username#, :email
has_many :items, through: :lists
has_many :lists
end
app/controllers/api/users_controller.rb
def index
@users = User.all
render json: @users, each_serializer: UserSerializer
end
路线 Rails.application.routes.draw做
namespace :api, defaults: { format: :json } do
resources :users do
resources :lists
end
end
end
让我知道是否可以更好地阐明它。谢谢!!
(来自评论的回答)
要使用 JSON API 适配器,您需要声明您要使用它。
ActiveModelSerializers.config.adapter = ActiveModelSerializers::Adapter::JsonApi
根据 AMS readme。
ActiveModelSerializers.config.adapter = :json_api # Default: `:attributes`
但是,这并没有解决我的问题。这是由于哈希被嵌套,如