我正在構建一個帶有Rails後端的JS應用程序,爲了不混淆蛇和駱駝事件,我想通過從服務器返回camelcase鍵名來規範化它。因此,當從API返回時,user.last_name
將返回user.lastName
。如何使用camelcased鍵名從Rails返回JSON
我該如何做到這一點?謝謝!
編輯:新增控制器代碼
class Api::V1::UsersController < API::V1::BaseController
# authorize_resource
respond_to :json, only: [:index]
def sky
@user = User.find_by_id(params[:user_id])
if @user
obj = {
sky: {
sectors: @user.sectors,
slots: @user.slots
}
}
render json: obj
else
raise "Unable to get Sky"
end
end
end
感謝moveson,我這樣做,並且我得到了JSON序列化版本'[active_model_serializers]加載ActiveModel渲染::串行::空與Hash'在我的控制檯。這裏的任何想法? –
難道是因爲如果我的繼承? '類的API :: V1 :: UsersController
FWIW,這是一個Rails應用程序,而不是一個軌道的API的應用程序 –