Versioning ActiveModel::Serializer

大憨熊 提交于 2019-12-13 19:53:40

问题


I'm using the gem active_model_serializers and I'm facing some issues with versioning.

Controllers

In app/controllers/v1/contracts_controller.rb

module V1
    class ContractsController < ApiController

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

    end
end

In app/controllers/v2/contracts_controller.rb

module V2
    class ContractsController < ApiController

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

    end
end

Serializers

In app/serializers/v1/contract_serializer.rb

class ContractSerializer < ActiveModel::Serializer
    attributes :id
end

In app/serializers/v2/contract_serializer.rb

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

Whether I call the route /v1/contracts or /v2/contracts, the rendered json include the contract name, which means that the serializer in v2 seems to be always called.

FYI, I added config.autoload_paths += Dir[Rails.root.join('app', 'serializers', '**/')] in config/application.rb


回答1:


You need to specify the serializers in your controllers, e.g. my answer here



来源:https://stackoverflow.com/questions/26159813/versioning-activemodelserializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!