[英]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.