Azaryan
Azaryan

Reputation: 328

active_model_serializers not working in rails-api

I have been looking around a lot and have not found a solution.

I have a rails-API application and simple model, controller, and serializer

but when I try to get index route I get standard rails JSON not serialized.

class Tag < ActiveRecord::Base
end

class TagSerializer < ActiveModel::Serializer
  attributes :id, :title
end

class TagsController < ApplicationController
  def index
    render json: Tag.all
  end
end

I get:

[
  tag: {
    id: 1,
    title: 'kifla'
  },
  tag: 
 .....

I want:

[
  { id: 1, title: 'kifla'},
  { .....

Upvotes: 5

Views: 12478

Answers (5)

Ahmed Aziz
Ahmed Aziz

Reputation: 1

Try restarting your browser, if you recently added the serializer

Upvotes: -1

OMGPOP
OMGPOP

Reputation: 932

If you are using the latest version, it seems that you can't set the default adapter anymore according to this:

https://github.com/rails-api/active_model_serializers/issues/1683

The workaround is to use

    render json: object_to_render, adapter: :json

Also, it's not necessary to add this anymore:

    include ::ActionController::Serialization

It's frustrated to see how Rails api keeps changing version to version without detailed documentation.

Upvotes: 4

rossta
rossta

Reputation: 11494

Looks like you're trying to disable the root element of your json output.

How this is achieved may depend on which version of active_model_serializers you're using.

In 0.9.x, you could do something like this in a Rails initializer:

# Disable for all serializers (except ArraySerializer)
ActiveModel::Serializer.root = false

# Disable for ArraySerializer
ActiveModel::ArraySerializer.root = false

Or simply, in your controller action:

class TagsController < ApplicationController
  def index
    render json: Tag.all, root: false
  end
end

For more info, here are links to relevant sections of the README pages of recent versions.

https://github.com/rails-api/active_model_serializers/tree/0-9-stable#disabling-the-root-element

https://github.com/rails-api/active_model_serializers/tree/0-8-stable#disabling-the-root-element

--

Note, please make sure also that you're actually including the code that handles serialization, as ActionController::API does not by default. For example,

class ApplicationController < ActionController::API
  include ActionController::Serialization
end

Upvotes: 21

Zamith
Zamith

Reputation: 23300

That's because the serialization is not loaded by default in rails-api. You have to do this:

class ApplicationController < ActionController::API
  include ::ActionController::Serialization
end

Upvotes: 33

anny_goerl
anny_goerl

Reputation: 642

I had the same problem, when using the most recent rails-api 4.2.0beta

Serializers didn't get picked up. I downgraded rails-api to 4.1.5 and it worked. Don't forget to remove

config.active_record.raise_in_transactional_callbacks = true

from config/application.rb it will raise an error in versions prior to 4.2.0

Upvotes: 4

Related Questions