Module: ActionController::ParamsWrapper
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Classes:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::ActiveSupport::Concern
|
|
Defined in: | actionpack/lib/action_controller/metal/params_wrapper.rb |
Overview
Wraps the parameters hash into a nested hash. This will allow clients to submit requests without having to specify any root elements.
This functionality is enabled by default for JSON, and can be customized by setting the format array:
class ApplicationController < ActionController::Base
wrap_parameters format: [:json, :xml]
end
You could also turn it on per controller:
class UsersController < ApplicationController
wrap_parameters format: [:json, :xml, :url_encoded_form, :multipart_form]
end
If you enable ParamsWrapper
for :json
format, instead of having to send JSON parameters like this:
{"user": {"name": "Konata"}}
You can send parameters like this:
{"name": "Konata"}
And it will be wrapped into a nested hash with the key name matching the controller’s name. For example, if you’re posting to UsersController
, your new params
hash will look like this:
{"name" => "Konata", "user" => {"name" => "Konata"}}
You can also specify the key in which the parameters should be wrapped to, and also the list of attributes it should wrap by using either :include
or :exclude
options like this:
class UsersController < ApplicationController
wrap_parameters :person, include: [:username, :password]
end
On Active Record models with no :include
or :exclude
option set, it will only wrap the parameters returned by the class method attribute_names
.
If you’re going to pass the parameters to an ::ActiveModel
object (such as ‘User.new(params)`), you might consider passing the model class to the method instead. The ParamsWrapper
will actually try to determine the list of attribute names from the model and only wrap those attributes:
class UsersController < ApplicationController
wrap_parameters Person
end
You still could pass :include
and :exclude
to set the list of attributes you want to wrap.
By default, if you don’t specify the key in which the parameters would be wrapped to, ParamsWrapper
will actually try to determine if there’s a model related to it or not. This controller, for example:
class Admin::UsersController < ApplicationController
end
will try to check if Admin::User
or User
model exists, and use it to determine the wrapper key respectively. If both models don’t exist, it will then fall back to use user
as the key.
To disable this functionality for a controller:
class UsersController < ApplicationController
wrap_parameters false
end
Constant Summary
-
EXCLUDE_PARAMETERS =
# File 'actionpack/lib/action_controller/metal/params_wrapper.rb', line 86%w(authenticity_token _method utf8)
Class Method Summary
::ActiveSupport::Concern
- Extended
class_methods | Define class methods from given block. |
included | Evaluate given block in context of base class, so that you can write class macros here. |
prepended | Evaluate given block in context of base class, so that you can write class macros here. |
append_features, prepend_features |
Instance Attribute Summary
-
#_wrapper_enabled? ⇒ Boolean
readonly
private
Checks if we should perform parameters wrapping.
Instance Method Summary
- #_extract_parameters(parameters) private
- #_perform_parameter_wrapping private
-
#_wrap_parameters(parameters)
private
Returns the list of parameters which will be selected for wrapped.
-
#_wrapper_formats
private
Returns the list of enabled formats.
-
#_wrapper_key
private
Returns the wrapper key which will be used to store wrapped parameters.
-
#process_action
private
Performs parameters wrapping upon the request.
DSL Calls
included
[ GitHub ]Instance Attribute Details
#_wrapper_enabled? ⇒ Boolean
(readonly, private)
Checks if we should perform parameters wrapping.
# File 'actionpack/lib/action_controller/metal/params_wrapper.rb', line 289
def _wrapper_enabled? return false unless request.has_content_type? ref = request.content_mime_type.ref _wrapper_formats.include?(ref) && _wrapper_key && !request.parameters.key?(_wrapper_key) rescue ActionDispatch::Http::Parameters::ParseError false end
Instance Method Details
#_extract_parameters(parameters) (private)
[ GitHub ]# File 'actionpack/lib/action_controller/metal/params_wrapper.rb', line 277
def _extract_parameters(parameters) if include_only = .include parameters.slice(*include_only) elsif .exclude exclude = .exclude + EXCLUDE_PARAMETERS parameters.except(*exclude) else parameters.except(*EXCLUDE_PARAMETERS) end end
#_perform_parameter_wrapping (private)
[ GitHub ]# File 'actionpack/lib/action_controller/metal/params_wrapper.rb', line 299
def _perform_parameter_wrapping wrapped_hash = _wrap_parameters request.request_parameters wrapped_keys = request.request_parameters.keys wrapped_filtered_hash = _wrap_parameters request.filtered_parameters.slice(*wrapped_keys) # This will make the wrapped hash accessible from controller and view. request.parameters.merge! wrapped_hash request.request_parameters.merge! wrapped_hash # This will display the wrapped hash in the log file. request.filtered_parameters.merge! wrapped_filtered_hash end
#_wrap_parameters(parameters) (private)
Returns the list of parameters which will be selected for wrapped.
# File 'actionpack/lib/action_controller/metal/params_wrapper.rb', line 273
def _wrap_parameters(parameters) { _wrapper_key => _extract_parameters(parameters) } end
#_wrapper_formats (private)
Returns the list of enabled formats.
# File 'actionpack/lib/action_controller/metal/params_wrapper.rb', line 268
def _wrapper_formats .format end
#_wrapper_key (private)
Returns the wrapper key which will be used to store wrapped parameters.
# File 'actionpack/lib/action_controller/metal/params_wrapper.rb', line 263
def _wrapper_key .name end
#process_action (private)
Performs parameters wrapping upon the request. Called automatically by the metal call stack.
# File 'actionpack/lib/action_controller/metal/params_wrapper.rb', line 257
def process_action(*) _perform_parameter_wrapping if _wrapper_enabled? super end