Search code examples
ruby-on-railsactive-model-serializers

Unknown adapter: json_api. Valid adapters are: [":fragmentcache", ":json", ":flattenjson", ":null", ":jsonapi"]


I have rails4 running with active_model_serializers (0.10.0.rc2) gem. Today I start developing the api for my client device. I run rspec test, It gave me this error

ArgumentError:
   Unknown adapter: json_api. Valid adapters are: [":fragmentcache", ":json", ":flattenjson", ":null", ":jsonapi"].

My config:

class BaseSerializer < ActiveModel::Serializer
  def initialize(object, options = {})
    ActiveModel::Serializer.config.adapter = :json_api
    super(object, options)
  end
end

I check the gem It is still 0.10.0.rc2. so no change in the gem list. I wonder what can cause this problem ?

please help


Solution

  • I found a solution from this thread https://github.com/rails-api/active_model_serializers/issues/993. my problem was that I have the 'API' acronym in my config/inflections.rb.

    inflect.acronym 'RSA'
    inflect.acronym 'API'
    

    The :json_api adapter will actuall be converted to a class. As api will be translated to API then AMS will not be able to find something like this JsonAPI.

    To solve this I have to put the class directly as below:

    class BaseSerializer < ActiveModel::Serializer
      def initialize(object, options = {})
        ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
        super(object, options)
      end
    end