-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Setting root key strategy globally for json adapter #1536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I've found a way to reproduce the issue when |
What about using the render json: @profile, serializer: UserProfileSerializer, root: "profile" ? |
I see two other solutions here:
class ProfileCollectionSerializer < ActiveModel::Serializer::CollectionSerializer
def json_key
"profiles"
end
end render json: @profiles, serializer: ProfileCollectionSerializer
class CollectionWrapper
include Enumerable
delegate :each, to: :@collection
attr_reader :name
def initialize(collection, name)
@collection = collection
@name = name
end
end render json: CollectionWrapper.new(@profiles, :profiles) |
@groyoh Those are all valid options, the only concern is that they force you to be explicit about how you define the root element name. I've created a pull requests which should allow you to implicitly use |
There is a
Which is addressed in your PR #1537 Thanks! |
Did #1618 resolve this? |
After updating to
v0.10.0.rc4
tag I had to specifyActiveModelSerializers.config.adapter = :json
in an initializer for a rails application in order to make active_model_serializer to add root key to the the response. However if the model is nested the key is built using the whole class name. For example if I specify a model:and then in controller:
It will build json containing the root key as follows:
{'users/profile': {'name': 'test'}}
. Is there any way to make active_model_serializer to use the class itself ignoring nesting? I've looked through the source code and found a methodjson_key
which builds the root element of the json. But overriding that method is not always working. Moving root element build method to a overridable strategy may be a good feature request candidateThe text was updated successfully, but these errors were encountered: