Module: ActionController::ParamsWrapper

Extended by:
ActiveSupport::Concern
Defined in:
lib/action_controller/metal/params_wrapper.rb

Overview

Wraps parameters hash into nested hash. This will allow client to submit POST request without having to specify a root element in it.

This functionality is enabled in config/initializers/wrap_parameters.rb and can be customized. If you are upgrading to Rails 3.1, this file will need to be created for the functionality to be enabled.

You could also turn it on per controller by setting the format array to non-empty array:

class UsersController < ApplicationController
  wrap_parameters :format => [:json, :xml]
end

If you enable ParamsWrapper for :json format. Instead of having to send JSON parameters like this:

{"user": {"name": "Konata"}}

You can now just send a parameters like this:

{"name": "Konata"}

And it will be wrapped into a nested hash with the key name matching 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

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 fallback to use user as the key.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

EXCLUDE_PARAMETERS =
%w(authenticity_token _method utf8)

Instance Method Summary collapse

Instance Method Details

#process_action(*args) ⇒ Object

Performs parameters wrapping upon the request. Will be called automatically by the metal call stack.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/action_controller/metal/params_wrapper.rb', line 189

def process_action(*args)
  if _wrapper_enabled?
    wrapped_hash = _wrap_parameters request.request_parameters
    wrapped_filtered_hash = _wrap_parameters request.filtered_parameters

    # This will make the wrapped hash accessible from controller and view
    request.parameters.merge! wrapped_hash
    request.request_parameters.merge! wrapped_hash

    # This will make the wrapped hash displayed in the log file
    request.filtered_parameters.merge! wrapped_filtered_hash
  end
  super
end