Reputation: 175
I'm trying to return the API response as lowerCamelCase but it is not working, I need to do this for all my Controllers/fields so I need a solution for entire project.
I've tried a lot of stuff, including this (http://brentvatne.ca/automatic-casing-activemodel-serializer/) who tells me to configure Activemodel to lower_camel as following
ActiveModel::Serializer.config.key_format = :lower_camel
But that is not working, it is returning the following json
{
"users": [{
"id": "56b110089c28691b84a3bd73",
"first_name": "Lucas"
}]
}
I need to transform the first_name into firstName.
Versions:
rails -v
Rails 4.2.5
ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [i386-mingw32]
And the gems
active_model_serializers (0.10.0.rc4)
rails-api (0.4.0)
My ember App recognize the JSON but I don't want to use snake case variables on JS
Upvotes: 8
Views: 4741
Reputation: 7604
Boom! I found it! I had to dig through the AMS repo (and eventually stumbled upon a helpful readme) but here it is for v0.10:
ActiveModelSerializers.config.key_transform = :camel_lower
Put that in an initializer.
There are also other options: :dash
, :camel
, :underscore
, and :unaltered
, and nil
Upvotes: 14
Reputation: 175
The problem was on the version of Active Model Serializer (0.10.0rc2).
On the last stable version (0.9) there was an issue that has been merged to fix the camelCase but this same PR is not present on the 0.10 RC versions.
So after I have downgraded the gem it worked :)
Upvotes: 2
Reputation: 1059
Search a project for key_format
in case it's overridden somewhere.
Please try it in the console, try to set key_format
explicitly to make sure it's possible (ex MySerializer.new(object, key_format: :lower_camel).as_json
)
If it doesn't help you can put here code example how you are using serializers.
Upvotes: 1