Class Index [+]

Quicksearch

ActionController::ParamsWrapper

Wraps the parameters hash into a nested hash. This will allow clients to submit POST requests without having to specify any root elements.

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 a 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 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 ActiveRecord models with no :include or :exclude option set, if attr_accessible is set on that model, it will only wrap the accessible parameters, else 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[:user])+), 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.

Constants

EXCLUDE_PARAMETERS

Public Instance Methods

process_action(*args) click to toggle source

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

     # File lib/action_controller/metal/params_wrapper.rb, line 194
194:     def process_action(*args)
195:       if _wrapper_enabled?
196:         wrapped_hash = _wrap_parameters request.request_parameters
197:         wrapped_keys = request.request_parameters.keys
198:         wrapped_filtered_hash = _wrap_parameters request.filtered_parameters.slice(*wrapped_keys)
199: 
200:         # This will make the wrapped hash accessible from controller and view
201:         request.parameters.merge! wrapped_hash
202:         request.request_parameters.merge! wrapped_hash
203: 
204:         # This will make the wrapped hash displayed in the log file
205:         request.filtered_parameters.merge! wrapped_filtered_hash
206:       end
207:       super
208:     end

Private Instance Methods

_wrap_parameters(parameters) click to toggle source

Returns the list of parameters which will be selected for wrapped.

     # File lib/action_controller/metal/params_wrapper.rb, line 223
223:       def _wrap_parameters(parameters)
224:         value = if include_only = _wrapper_options[:include]
225:           parameters.slice(*include_only)
226:         else
227:           exclude = _wrapper_options[:exclude] || []
228:           parameters.except(*(exclude + EXCLUDE_PARAMETERS))
229:         end
230: 
231:         { _wrapper_key => value }
232:       end
_wrapper_enabled?() click to toggle source

Checks if we should perform parameters wrapping.

     # File lib/action_controller/metal/params_wrapper.rb, line 235
235:       def _wrapper_enabled?
236:         ref = request.content_mime_type.try(:ref)
237:         _wrapper_formats.include?(ref) && _wrapper_key && !request.request_parameters[_wrapper_key]
238:       end
_wrapper_formats() click to toggle source

Returns the list of enabled formats.

     # File lib/action_controller/metal/params_wrapper.rb, line 218
218:       def _wrapper_formats
219:         _wrapper_options[:format]
220:       end
_wrapper_key() click to toggle source

Returns the wrapper key which will use to stored wrapped parameters.

     # File lib/action_controller/metal/params_wrapper.rb, line 213
213:       def _wrapper_key
214:         _wrapper_options[:name]
215:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.