Skip to content

Instantly share code, notes, and snippets.

@vochicong
Last active December 24, 2017 12:23
Show Gist options
  • Save vochicong/4e14a6764ed69c9c5c5d1f885dc85942 to your computer and use it in GitHub Desktop.
Save vochicong/4e14a6764ed69c9c5c5d1f885dc85942 to your computer and use it in GitHub Desktop.
# File: config/initializers/params_snakeizer.rb
# Transform JSON request param keys from JSON-conventional camelCase to
# Rails-conventional snake_case
module ActionController
# Modified from action_controller/metal/strong_parameters.rb
class Parameters
def deep_snakeize!
@parameters.deep_transform_keys!(&:underscore)
self
end
end
end
# File: config/initializers/params_snakeizer.rb
# Transform JSON request param keys from JSON-conventional camelCase to
# Rails-conventional snake_case
module ActionController
# Modified from action_controller/metal/strong_parameters.rb
class Parameters
def deep_snakeize!
@parameters.deep_transform_keys!(&:underscore)
self
end
end
end
# File: app/controllers/application_controller.rb
class ApplicationController < ActionController::API
protected
# Snakeize JSON API request params
def snakeize_params
params.deep_snakeize!
end
end
# File: app/controllers/application_controller.rb
class ApplicationController < ActionController::API
protected
# Snakeize JSON API request params
def snakeize_params
params.deep_snakeize!
end
end
class UsersController < ApplicationController
before_action :snakeize_params, only: [:say_hello, :say_goodbye]
# .....
end
class UsersController < ApplicationController
before_action :snakeize_params, only: [:say_hello, :say_goodbye]
# .....
end
# File: app/controllers/application_controller.rb
class ApplicationController < ActionController::API
before_action :snakeize_params
protected
# Snakeize JSON API request params
def snakeize_params
params.deep_snakeize!
end
end
# File: app/controllers/application_controller.rb
class ApplicationController < ActionController::API
before_action :snakeize_params
protected
# Snakeize JSON API request params
def snakeize_params
params.deep_snakeize!
end
end
json.key_format! camelize: :lower
json.key_format! camelize: :lower
Jbuilder.key_format camelize: :lower
Jbuilder.key_format camelize: :lower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment