Ruby API响应为小写驼峰 [英] Ruby API response as lower camel case
问题描述
我正在尝试将API响应作为lowerCamelCase返回,但是它不起作用,我需要对我所有的Controllers/fields都执行此操作,因此我需要整个项目的解决方案.
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.
我尝试了很多东西,包括这个( http://brentvatne.ca /automatic-casing-activemodel-serializer/),他告诉我将Activemodel配置为lower_camel,如下所示:
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
但这不起作用,它返回以下json
But that is not working, it is returning the following json
{
"users": [{
"id": "56b110089c28691b84a3bd73",
"first_name": "Lucas"
}]
}
我需要将first_name转换为firstName.
I need to transform the first_name into firstName.
版本:
rails -v
Rails 4.2.5
ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [i386-mingw32]
还有宝石
active_model_serializers (0.10.0.rc4)
rails-api (0.4.0)
我的ember应用程序可以识别JSON,但是我不想在JS上使用蛇形变量
My ember App recognize the JSON but I don't want to use snake case variables on JS
推荐答案
问题出在Active Model Serializer(0.10.0rc2)的版本上.
The problem was on the version of Active Model Serializer (0.10.0rc2).
在最后一个稳定版本( 0.9 )上,已合并以修复camelCase的问题在0.10 RC版本上.
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 :)
这篇关于Ruby API响应为小写驼峰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!