【问题标题】:Versioning ActiveModel::Serializer版本控制 ActiveModel::Serializer
【发布时间】:2014-10-02 11:27:53
【问题描述】:

我正在使用 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', '**/')]

【问题讨论】:

    标签: ruby-on-rails activemodel active-model-serializers


    【解决方案1】:

    您需要在控制器中指定序列化程序,例如my answer here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多