instance method
fetch
Ruby on Rails 7.2.3
Since v4.0.13Signature
fetch(key, *args)
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: none params.fetch(:none, {}) # => #<ActionController::Parameters {} permitted: false> params.fetch(:none, "Francesco") # => "Francesco" params.fetch(:none) { "Francesco" } # => "Francesco"
Parameters
-
keyreq -
argsrest
Source
# File actionpack/lib/action_controller/metal/strong_parameters.rb, line 693
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
Defined in actionpack/lib/action_controller/metal/strong_parameters.rb line 693
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionController::Parameters