簡體   English   中英

版本控制ActiveModel :: Serializer

[英]Versioning ActiveModel::Serializer

我正在使用gem active_model_serializers,並且遇到版本控制方面的問題。

控制器

app/controllers/v1/contracts_controller.rb

module V1
    class ContractsController < ApiController

        def index
            @contracts = Contract.all
            render json: @contracts
        end

    end
end

app/controllers/v2/contracts_controller.rb

module V2
    class ContractsController < ApiController

        def index
            @contracts = Contract.all
            render json: @contracts
        end

    end
end

串行器

app/serializers/v1/contract_serializer.rb

class ContractSerializer < ActiveModel::Serializer
    attributes :id
end

app/serializers/v2/contract_serializer.rb

class ContractSerializer < ActiveModel::Serializer
    attributes :id, :name
end

無論我將路由稱為/v1/contracts還是/v2/contracts ,呈現的json都包含合同名稱,這意味着v2中的序列化程序似乎總是被調用。

僅供參考,我在config/application.rb添加了config.autoload_paths += Dir[Rails.root.join('app', 'serializers', '**/')]

您需要在控制器中指定序列化器,例如我的答案在這里

暫無
暫無

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

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