簡體   English   中英

JSON-API-資源:如何獲取與特定對象相關的數據

[英]JSON-API-Resources: How can I get the data related the specific object

如何使用JSON-API-Resources獲取與特定對象相關的數據。

在控制器內

module Api
  module V1
    class SubscriptionsController < Api::V1::ApiController

      def create
        @plan = Plan.find_by(id: plan_id_param)
        @result = Creditcard::CreateSubscription.call(@plan, params_ex: foo)

        if @result.errors.blank?
          resource = Api::V1::SubscriptionResource.new(@result, nil)
          serializer = JSONAPI::ResourceSerializer.new(Api::V1::SubscriptionResource)
          json_body = serializer.serialize_to_hash(resource)
          render json: json_body, status: 200 # :ok

通常,我們可以做

  def show
    params[:include] = "relation1.relation2.relation3" if params[:include].nil?
    super
  end

完成了。 但是在這種情況下,這是JSONAPI::SubscriptionResource對象。 如何顯示對象的關系?

  resource = Api::V1::SubscriptionResource.new(@result, nil)
  serializer = JSONAPI::ResourceSerializer.new(Api::V1::SubscriptionResource, include: ['foo_xxx.hoo_xxx'])

我發現,在這種情況下,您必須使用_而不是-

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM