Ruby on rails 4 ActiveModel::序列化程序定义属性?

Ruby on rails 4 ActiveModel::序列化程序定义属性?,ruby-on-rails-4,active-model-serializers,Ruby On Rails 4,Active Model Serializers,我正在尝试为我的Rails应用程序实现JSON API。它需要定义字段。但是ActiveModel::Serializer有一个同名的方法,因此 class FooSerializer < ActiveModel::Serializer attributes :attributes def attributes { # to be filled } end end class FooSerializer

我正在尝试为我的Rails应用程序实现JSON API。它需要定义字段。但是
ActiveModel::Serializer
有一个同名的方法,因此

class FooSerializer < ActiveModel::Serializer
  attributes :attributes

  def attributes
    {
      # to be filled
    }
  end
end
class FooSerializer

将只重写原始方法。有没有可能以某种方式添加属性字段?

ActiveModel序列化程序支持JSON API开箱即用。您只需要设置正确的适配器

ActiveModelSerializers.config.adapter = :json_api

以下是操作方法:

class FooSerializer < ActiveModel::Serializer
  def attributes(*args)
    hash = super
    hash[:attributes] = attributes_list
    hash
  end

  def attributes_list
    {
      # to be filled
    }
  end
end
class FooSerializer
它生成一个JSON对象,但不创建属性容器。您使用的是哪个版本的AMS。也可以显示AMS初始化器文件我使用AMS v 0.9.3 config/initializers/active_model_serializer.rb
ruby ActiveModel::serializer.setup do | config | config.adapter=:json_api end
ahh好的,我认为json_api支持仅在0.10中。我建议您使用它(它已经在RC中了),否则您可以使用中列出的其他库之一。在国际海事组织,蒙基帕奇将更痛苦:)