Methods
Instance Public methods
Sets the default wrapper key or model which will be used to determine wrapper key and attribute names. Will be called automatically when the module is inherited.
Sets the name of the wrapper key, or the model which ParamsWrapper would use to determine the attribute names from.
Examples
wrap_parameters :format => :xml # enables the parameter wrapper for XML format wrap_parameters :person # wraps parameters into +params[:person]+ hash wrap_parameters Person # wraps parameters by determining the wrapper key from Person class (+person+, in this case) and the list of attribute names wrap_parameters :include => [:username, :title] # wraps only +:username+ and +:title+ attributes from parameters. wrap_parameters false # disables parameters wrapping for this controller altogether.
Options
- :format - The list of formats in which the parameters wrapper will be enabled.
- :include - The list of attribute names which parameters wrapper will wrap into a nested hash.
- :exclude - The list of attribute names which parameters wrapper will exclude from a nested hash.
# File actionpack/lib/action_controller/metal/params_wrapper.rb, line 111 111: def wrap_parameters(name_or_model_or_options, options = {}) 112: model = nil 113: 114: case name_or_model_or_options 115: when Hash 116: options = name_or_model_or_options 117: when false 118: options = options.merge(:format => []) 119: when Symbol, String 120: options = options.merge(:name => name_or_model_or_options) 121: else 122: model = name_or_model_or_options 123: end 124: 125: _set_wrapper_defaults(_wrapper_options.slice(:format).merge(options), model) 126: end
Instance Protected methods
# File actionpack/lib/action_controller/metal/params_wrapper.rb, line 165 165: def _set_wrapper_defaults(options, model=nil) 166: options = options.dup 167: 168: unless options[:include] || options[:exclude] 169: model ||= _default_wrap_model 170: role = options.has_key?(:as) ? options[:as] : :default 171: if model.respond_to?(:accessible_attributes) && model.accessible_attributes(role).present? 172: options[:include] = model.accessible_attributes(role).to_a 173: elsif model.respond_to?(:attribute_names) && model.attribute_names.present? 174: options[:include] = model.attribute_names 175: end 176: end 177: 178: unless options[:name] || self.anonymous? 179: model ||= _default_wrap_model 180: options[:name] = model ? model.to_s.demodulize.underscore : 181: controller_name.singularize 182: end 183: 184: options[:include] = Array.wrap(options[:include]).collect(&:to_s) if options[:include] 185: options[:exclude] = Array.wrap(options[:exclude]).collect(&:to_s) if options[:exclude] 186: options[:format] = Array.wrap(options[:format]) 187: 188: self._wrapper_options = options 189: end