19

If I want to switch from the default attributes adapter to the JSON API adapter, where would I do this?

The getting started states this:

Generally speaking, you as a user of AMS will write (or generate) these serializer classes. If you want to use a different adapter, such as a JSON API, you can change this in an initializer:

ActiveModel::Serializer.config.adapter = :json_api

What initializer are they referring to? Do I create a new one? Sorry for the noob question

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
Jshoe523
  • 3,851
  • 2
  • 18
  • 21

3 Answers3

20

In general, initializers are put under the app/config/initializers directory in a Rails app.

So, in your case, you can create a new file there: ams.rb and put those content in that file:

# app/config/initializers/ams.rb    
ActiveModel::Serializer.config.adapter = :json_api

Also, see this github issue.

If you want to be using the :json_api format, you have to use the 0.10.0 branch off of Github.

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • Hey thanks for the reply. So I tried that earlier and I get an undefined method `config' for ActiveModel::Serializer:Class...am I missing a step from just creating the initializers file? – Jshoe523 Nov 10 '15 at 00:35
  • which version of AMS are you using? – K M Rakibul Islam Nov 10 '15 at 00:37
  • See this: https://github.com/rails-api/active_model_serializers/issues/803 `If you want to be using the :json_api format, you have to use the 0.10.0 branch off of Github` – K M Rakibul Islam Nov 10 '15 at 00:39
  • uninitialized constant ActiveModelSerializers (NameError), I have put `require 'active_model_serializers' ActiveModelSerializers.config.adapter = :json_api ` in config/initializers/ams.rb, throwing above error when restarting rails server – kamal Aug 08 '16 at 04:59
5

For newer version of AMS put this to config/initializers/ams.rb:

require 'active_model_serializers'

ActiveModelSerializers.config.adapter = :json_api
EugZol
  • 6,476
  • 22
  • 41
  • uninitialized constant ActiveModelSerializers (NameError), I have put require 'active_model_serializers' ActiveModelSerializers.config.adapter = :json_api in config/initializers/ams.rb, throwing above error when restarting rails server – kamal Aug 08 '16 at 05:01
-1

Create a new file with any name inside app/config/initializers/ and add:

ActiveModelSerializers.config.adapter = :json_api

All the files under initalizers directory run during initialization irrespective of the file name.

slal
  • 2,657
  • 18
  • 29