Method: ActionController::Parameters#fetch

Defined in:
actionpack/lib/action_controller/metal/strong_parameters.rb

#fetch(key, *args) ⇒ Object

Returns a parameter for the given ‘key`. If the `key` can’t be found, there are several options: With no other arguments, it will raise an ActionController::ParameterMissing error; if a second argument is given, then that is returned (converted to an instance of ‘ActionController::Parameters` if possible); if a block is given, then that will be run and its result returned.

params = ActionController::Parameters.new(person: { name: "Francesco" })
params.fetch(:person)               # => #<ActionController::Parameters {"name"=>"Francesco"} permitted: false>
params.fetch(:none)                 # => ActionController::ParameterMissing: param is missing or the value is empty or invalid: none
params.fetch(:none, {})             # => #<ActionController::Parameters {} permitted: false>
params.fetch(:none, "Francesco")    # => "Francesco"
params.fetch(:none) { "Francesco" } # => "Francesco"


820
821
822
823
824
825
826
827
828
829
830
# File 'actionpack/lib/action_controller/metal/strong_parameters.rb', line 820

def fetch(key, *args)
  convert_value_to_parameters(
    @parameters.fetch(key) {
      if block_given?
        yield
      else
        args.fetch(0) { raise ActionController::ParameterMissing.new(key, @parameters.keys) }
      end
    }
  )
end