How to disable ActiveModel :: Serializers for a specific controller?

We use active_model_serializers - 0.8.1 in the Rails application.

There are some API-specific controllers in the application that inherit from ActionController :: Metal in a manner similar to the rails-api ActionController :: API .

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

Note:
As mentioned in the documentation, using a serializer can be explicitly avoided by replacing

render :json

with:

render :json => @your_object.to_json

We are looking for a more elegant solution than the previous one.
Thanks.

+6
source share
5 answers

You can also do:

 respond_with @your_object, serializer: nil 
+5
source

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

 def default_serializer_options { serializer: nil } end 

We are currently creating JSON in other ways, but I wanted to explore ActiveModel::Serializers for a specific resource. This is when I discovered that adding a gem means that all controllers will try to use serializers. Not only the one in which I wanted to test the approach.

Using serializers by default is in full swing, but it will take us some time to move existing controllers to this approach. I updated these other existing controllers to not use serializers (using the above method), so I can add controller-based serializers on the controller in the future.

+4
source

I had a problem that also required disabling serialization, so I created this transfer request , which adds the ability to disable serialization in a specific controller, causing disable_serialization in the controller.

+2
source

I do not know if there is an elegant solution. It looks like you have to defuse ActionController :: Serialization - https://github.com/rails-api/active_model_serializers/blob/5a92e00b51927c9c0e7c90f92c825aff09314bfd/lib/action_controller/serialization.rb .

The minimal change is likely to be due to overriding the build_json_serializer method and returning nil in controllers where you don't want to use ActiveModelSerializers. If build_json_serializer in the controller always returns nil, then the behavior should be the default for non-AMS serialization

Unfortunately, this class does not look well structured for modification (private methods, etc.), so you can send a transfer request to make the task easier. Either storing an alias for the original _render_option_json method before enabling AMS, making build_json_serializer protected rather than private, or adding a configuration parameter to disable AMS based on each controller, all will be sensible changes.

+1
source

It’s just the head that the @dbj answer, although correct, will not work in Rails4 due to the fact that response_with has been moved.

NoMethodError: The respond_with function was extracted to the responders stone. Add it to your Gemfile to continue using this feature: gem 'responseers', '~> 2.0' For more details, see the Rails Upgrade Guide.

The simple fix found by assuming, indeed, was this:

render json: @you_object, serializer: nil

0
source

Source: https://habr.com/ru/post/958495/


All Articles