在Rails中将所有控制器参数从camelCase转换为snake_case的最佳方法是什么? [英] What is the best way to convert all controller params from camelCase to snake_case in Rails?

查看:94
本文介绍了在Rails中将所有控制器参数从camelCase转换为snake_case的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您已经知道,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的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆