跳至内容 跳至搜索
方法
#
W

实例公共方法

_set_wrapper_options(options)

# File actionpack/lib/action_controller/metal/params_wrapper.rb, line 189
def _set_wrapper_options(options)
  self._wrapper_options = Options.from_hash(options)
end

inherited(klass)

设置默认包装器键或模型,用于确定包装器键和属性名称。在模块继承时自动调用。

# File actionpack/lib/action_controller/metal/params_wrapper.rb, line 244
def inherited(klass)
  if klass._wrapper_options.format.any?
    params = klass._wrapper_options.dup
    params.klass = klass
    klass._wrapper_options = params
  end
  super
end

wrap_parameters(name_or_model_or_options, options = {})

设置包装器键的名称,或 ParamsWrapper 用于从其中确定属性名称的模型。

示例

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.

选项

  • :format - 参数包装器将启用的格式列表。

  • :include - 参数包装器将包装到嵌套哈希中的属性名称列表。

  • :exclude - 参数包装器将从嵌套哈希中排除的属性名称列表。

# File actionpack/lib/action_controller/metal/params_wrapper.rb, line 221
def wrap_parameters(name_or_model_or_options, options = {})
  model = nil

  case name_or_model_or_options
  when Hash
    options = name_or_model_or_options
  when false
    options = options.merge(format: [])
  when Symbol, String
    options = options.merge(name: name_or_model_or_options)
  else
    model = name_or_model_or_options
  end

  opts = Options.from_hash _wrapper_options.to_h.slice(:format).merge(options)
  opts.model = model
  opts.klass = self

  self._wrapper_options = opts
end