When I use:
render json: { status: 1, result: UserSerializer.new(user) }
Log file output:
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.37ms)
I don't want it, how can I fix the bug?
ActiveModelSerializers Version (commit ref if not on tag): 0.10.0
Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]
OS Type & Version: macOS Sierra
Integrated application and version (e.g., Rails, Grape, etc): Rails 5.0.0.1
Hi @hanluner! I'm not entirely sure what you're asking here. Do you want to get rid of the log output?
I want to know how I can disable serializer automatically when I custom serializer like 'UserSerializer.new(user)'.
I don't want to serialize null ,because 'Rendered ActiveModel::Serializer::Null with Hash' also cost some time.
@hanluner Doing something like render json: { status: 1, result: UserSerializer.new(user) } requires some understanding of what you're doing, since it's not a normal usage for AMS. Maybe that worked in an earlier version. I don't know. But, I think what you're trying to do is render json: { status: 1, result: UserSerializer.new(user).as_json } which is more or less equivalent to serializing with the attributes adapter, as long as you're not using caching.
You're getting that message because you're passing in a hash { status: 1, result: user_json }, which has no AMS serializer. (nor can it). I'm actually not sure why it's being processed by AMS at all.
@bf4 Thank you for your answer. But I used status to mark mobile develop things, so I can't remove it from json. I want to user AMS to control json serializer. I think AMS maybe offer the feature of custom json serializer without null serializer.
@hanluner I didn't ask you to remove the status from the response body. I said that AMS doesn't support your syntax, explained why, and then suggested an alternative. Otherwise, you'll have to read the docs. There are only a few maintainers. :)
Please review what I wrote above, and if that doesn't help, review the docs, and if that doesn't help, come back here and write out what you've tried.
Most helpful comment
@hanluner I didn't ask you to remove the
statusfrom the response body. I said that AMS doesn't support your syntax, explained why, and then suggested an alternative. Otherwise, you'll have to read the docs. There are only a few maintainers. :)Please review what I wrote above, and if that doesn't help, review the docs, and if that doesn't help, come back here and write out what you've tried.