Make root node in Active Model Serializer
This was covered in RailsCast #409 Active Model Serializers.
In order to remove the root node, you add root: false
in the call to render
in your controller. Assuming your contact
s in JSON come from a contacts#index
method, your code may look something like:
def index
@contacts = Contacts.all
respond_to do |format|
format.html
format.json { render json: @contacts, root: false }
end
end
Or, if you don't want any root nodes in any of your JSON, in your ApplicationController
, add the following method:
def default_serializer_options
{root: false}
end
For people using ActiveModel::Serializer v0.10.x, you will need to create an initializer and include the following:
# config/initializers/serializer.rb
ActiveModelSerializers.config.adapter = :json
ActiveModelSerializers.config.json_include_toplevel_object = true
Then, just restart your app and you should get the root objects you desire.
This works in Rails 5.1.x. YMMV. HTH.