class ActionController::Parameters
def require(key)
end
end
person_params.require(:name) # SAFER
params.require(:person).permit(:name).tap do |person_params|
def person_params
but take into account that at some point those ones have to be permitted:
name = params.require(:person).require(:name) # CAREFUL
params = ActionController::Parameters.new(person: { name: "Finn" })
# CAREFUL
Technically this method can be used to fetch terminal values:
# ActionController::ParameterMissing: param is missing or the value is empty: user
user_params, profile_params = params.require([:user, :profile])
params = ActionController::Parameters.new(user: {}, profile: {})
Otherwise, the method re-raises the first exception found:
user_params, profile_params = params.require([:user, :profile])
params = ActionController::Parameters.new(user: { ... }, profile: { ... })
returned:
in order. If it succeeds, an array with the respective return values is
When given an array of keys, the method tries to require each one of them
# ActionController::ParameterMissing: param is missing or the value is empty: person
ActionController::Parameters.new(person: {}).require(:person)
# ActionController::ParameterMissing: param is missing or the value is empty: person
ActionController::Parameters.new(person: "\t").require(:person)
# ActionController::ParameterMissing: param is missing or the value is empty: person
ActionController::Parameters.new(person: nil).require(:person)
# ActionController::ParameterMissing: param is missing or the value is empty: person
ActionController::Parameters.new.require(:person)
Otherwise raises ActionController::ParameterMissing:
# => #
ActionController::Parameters.new(person: { name: "Francesco" }).require(:person)
either present or the singleton +false+, returns said value:
When passed a single key, if it exists and its associated value is
This method accepts both a single key and an array of keys.
def require(key) return key.map { |k| require(k) } if key.is_a?(Array) value = self[key] if value.present? || value == false value else raise ParameterMissing.new(key, @parameters.keys) end end