How to disable ActiveModel::Serializers for a specific controller?
Asked Answered
A

7

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.

Ambagious answered 20/11, 2013 at 16:18 Comment(2)
Could you give a code example that illustrates what you're aiming for? I think that would help greatly clarify your question.Cogitative
@Davidann we wished to be able to whitelist controllers on which active_model_serializers act via an option.Ambagious
S
6

You can also do:

respond_with @your_object, serializer: nil
Sensualist answered 20/3, 2015 at 18:52 Comment(1)
It seems like the syntax for this has changed: github.com/rails-api/active_model_serializers/issues/2221Afterbody
A
6

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

Admiration answered 14/11, 2019 at 10:22 Comment(0)
R
5

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.

Rouge answered 29/3, 2016 at 23:2 Comment(0)
H
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.

Hermann answered 25/11, 2013 at 22:40 Comment(0)
B
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).

Brenna answered 13/6, 2018 at 3:22 Comment(0)
A
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.

Alchemize answered 20/11, 2013 at 16:44 Comment(0)
K
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

Kop answered 11/8, 2016 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.