JSONAPI :: Resources:JSON有效负载中不存在数据根

问题描述

我正在尝试通过遵循active_model_serializers来将jsonnapi-resources替换为docs方法,但是它确实可以按需工作。更准确地说,根据JSON API,响应应包含data作为顶层元素。事实并非如此。 这是我更改的内容

  • 在文档中将app/resources/文件夹创建为required
  • PostResource文件夹中创建了app/resources
class PostResource < JSONAPI::Resource
  attributes :title,:body
end
  • 修改routes.rb以使用jsonapi_resources
Rails.application.routes.draw do
  jsonapi_resources :posts
end
class ApplicationController < ActionController::API
  include JSONAPI::ActsAsResourceController
end
  • PostsController#index操作中没有变化:
class PostsController < ApplicationController
  before_action :set_post,only: %i[show update destroy]

  # GET /posts
  def index
    @posts = Post.all
    render json: @posts
  end
...

启动服务器rails s并卷曲posts端点时:

curl -i -H "Accept: application/vnd.api+json" "http://localhost:3000/posts" 

HTTP/1.1 200 OK
x-frame-options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: strict-origin-when-cross-origin
Content-Type: application/json; charset=utf-8
ETag: W/"0d1196e0f0131056957b97d4e7da7138"
Cache-Control: max-age=0,private,must-revalidate
X-Request-Id: a2e739d5-461e-4ea1-a3ae-c100df11d7bb
X-Runtime: 0.006308
vary: Origin
X-Rack-CORS: miss; no-origin
transfer-encoding: chunked

[{"id":1,"title":"About Ruby","body":"It is a awesome language","created_at":"2020-11-08T17:52:44.202Z","updated_at":"2020-11-08T17:52:44.202Z"}]%   

没有data元素,它只是标准的JSON响应,不符合JSON API。

我想念什么?

用于:

  • 滑行6.0.3.4
  • Ruby 2.7.0

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)