问题描述
您已经知道,JSON命名约定提倡使用camelSpace,Rails提倡对参数名称使用snake_case.
As you already know, JSON naming convention advocates the use of camelSpace and the Rails advocates the use of snake_case for parameter names.
在Rails控制器中将所有请求的参数转换为snake_case的最佳方法是什么?
What is the best way to convert all request's params to snake_case in a rails controller?
从此:
{
...
"firstName": "John",
"lastName": "Smith",
"moreInfo":
{
"mealType": 2,
"mealSize": 4,
...
}
}
对此:
{
...
"first_name": "John",
"last_name": "Smith",
"more_info":
{
"meal_type": 2,
"meal_size": 4,
...
}
}
谢谢
推荐答案
完成以下步骤后,通过JSON请求提交的camelCase
参数名称将更改为snake_case
.
When you've completed the steps below, camelCase
param names submitted via JSON requests will be changed to snake_case
.
例如,名为passwordConfirmation
的JSON请求参数将在控制器中以params[:password_confirmation]
For example, a JSON request param named passwordConfirmation
would be accessed in a controller as params[:password_confirmation]
在config/initializers/json_param_key_transform.rb
处创建一个初始化程序.该文件将仅更改JSON请求的参数解析行为(JSON请求必须具有请求标头Content-Type: application/json
).
Create an initializer at config/initializers/json_param_key_transform.rb
. This file is going to change the parameter parsing behaviour for JSON requests only (JSON requests must have the request header Content-Type: application/json
).
找到您的Rails版本并选择下面的相应部分(在Gemfile.lock
中找到您的Rails版本):
Find your Rails version and choose the appropriate section below (find your Rails version in Gemfile.lock
):
对于Rails 5,要将驼峰式参数密钥转换为蛇形,请将其放在初始化程序中:
For Rails 5, to convert camel-case param keys to snake-case, put this in the initializer:
# File: config/initializers/json_param_key_transform.rb
# Transform JSON request param keys from JSON-conventional camelCase to
# Rails-conventional snake_case:
ActionDispatch::Request.parameter_parsers[:json] = -> (raw_post) {
# Modified from action_dispatch/http/parameters.rb
data = ActiveSupport::JSON.decode(raw_post)
data = {:_json => data} unless data.is_a?(Hash)
# Transform camelCase param keys to snake_case:
data.deep_transform_keys!(&:underscore)
}
对于Rails 4.2(可能还有更早的版本)
对于Rails 4.2(可能还有更早的版本),要将驼峰式参数密钥转换为蛇形,请将其放在初始化程序中:
For Rails 4.2 (and maybe earlier versions)
For Rails 4.2 (and maybe earlier versions), to convert camel-case param keys to snake-case, put this in the initializer:
# File: config/initializers/json_param_key_transform.rb
# Transform JSON request param keys from JSON-conventional camelCase to
# Rails-conventional snake_case:
Rails.application.config.middleware.swap(
::ActionDispatch::ParamsParser, ::ActionDispatch::ParamsParser,
::Mime::JSON => Proc.new { |raw_post|
# Borrowed from action_dispatch/middleware/params_parser.rb except for
# data.deep_transform_keys!(&:underscore) :
data = ::ActiveSupport::JSON.decode(raw_post)
data = {:_json => data} unless data.is_a?(::Hash)
data = ::ActionDispatch::Request::Utils.deep_munge(data)
# Transform camelCase param keys to snake_case:
data.deep_transform_keys!(&:underscore)
data.with_indifferent_access
}
)
所有Rails版本的最后一步
重新启动rails server
.
这篇关于在Rails中将所有控制器参数从camelCase转换为snake_case的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!