11

We're using active_model_serializers - 0.8.1 in a Rails application.

The app has some API specific controllers inheriting from ActionController::Metal in a way similar to rails-api's ActionController::API.

Well we want to use ActiveModel::Serializers only for the API controllers mentioned above.
Is that possible, how?

Note:
As mentioned in the documentation use of a serializer can explicitly be avoided by replacing

render :json

with:

render :json => @your_object.to_json

We are seeking a more elegant solution than the one above.
Thanks.

Dimitris Zorbas
  • 5,187
  • 3
  • 27
  • 33

7 Answers7

6

You can also do:

respond_with @your_object, serializer: nil
dbj
  • 69
  • 1
  • 2
  • 2
    It seems like the syntax for this has changed: https://github.com/rails-api/active_model_serializers/issues/2221 – oowowaee Mar 29 '18 at 19:59
5

For people who encounter this question in the future, adding adapter: nil to the render: json... will work (instead of serializer: nil)

manh.vu
  • 335
  • 1
  • 4
  • 14
4

You can override the default_serializer_options method on a controller to disable serializers for all actions in that controller.

def default_serializer_options
  {
    serializer: nil
  }
end

We're currently generating our JSON in other ways, but I wanted to investigate ActiveModel::Serializers for a particular resource. This is when I discovered that adding the gem means that all controllers will try to use serializers. Not just the one in which I wanted to test the approach.

Using the serializers by default is great going forward, but we'll need some time to transition existing controllers to this approach. I updated those other existing controllers to not use the serializers (using the above method), so I can add the serializers on a controller-by-controller basis going forward.

Kyle Tolle
  • 694
  • 2
  • 7
  • 17
2

I was having an issue that also required disabling serialization, so I created this pull request that adds the ability to disable serialization in a specific controller by calling disable_serialization in a controller.

josh
  • 9,656
  • 4
  • 34
  • 51
2

I know the question is kind of old and the solutions offered here are outdated just wanted to share my solution to have ActiveModel::Serializers and FastJsonApi by Netflix

The solution I made works for ActiveModel::Serializers 0.10.

# frozen_string_literal: true

class ApplicationController < ActionController::API

  private

  def _render_with_renderer_json(json, options)
    json = json.to_json(options) unless json.kind_of?(String)
    self.content_type ||= Mime[:json]
    json
  end
end

I just restored the original method _render_with_renderer_json of Ruby on Rails 5.x (https://github.com/rails/rails/blob/5-2-stable/actionpack/lib/action_controller/metal/renderers.rb#L156-L169).

Geoffrey
  • 193
  • 3
  • 11
1

I don't know if there's an elegant solution. It looks like you'd have to monkeypatch ActionController::Serialization - https://github.com/rails-api/active_model_serializers/blob/5a92e00b51927c9c0e7c90f92c825aff09314bfd/lib/action_controller/serialization.rb.

The minimal change would probably by overriding the build_json_serializer method and returning nil in the controllers where you don't want to use ActiveModelSerializers. If build_json_serializer in a controller always returns nil, then the behavior should default to non-AMS serialization

Unfortunately this class doesn't seem well structured for modification (private methods, etc.) so you may want to submit a pull request to make your task easier. Either keeping an alias to the 'original' _render_option_json method before AMS inclusion, making the build_json_serializer protected rather than private, or adding a config option to disable AMS on a per-controller basis would all be reasonable modifications.

Peter Goldstein
  • 4,479
  • 2
  • 19
  • 17
1

Just a heads up that @dbj's answer, while correct, won't work in Rails4 due to the fact that respond_with was moved.

NoMethodError: The respond_with feature has been extracted to the responders gem. Add it to your Gemfile to continue using this feature: gem 'responders', '~> 2.0' Consult the Rails upgrade guide for details.

The simple fix, found by guessing really, was to just do:

render json: @you_object, serializer: nil

apanzerj
  • 75
  • 1
  • 7