问题
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