Changing Active Model Serializers Default Adapter

眉间皱痕 提交于 2019-11-29 11:33:51

问题


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 JsonApi, 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


回答1:


In general, initilizers 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.




回答2:


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

require 'active_model_serializers'

ActiveModelSerializers.config.adapter = :json_api



回答3:


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.



来源:https://stackoverflow.com/questions/33620859/changing-active-model-serializers-default-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!