require
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (38)
- 5.1.7 (1)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
require(key)
public
This method accepts both a single key and an array of keys.
When passed a single key, if it exists and its associated value is either present or the singleton false, returns said value:
ActionController::Parameters.new(person: { name: "Francesco" }).require(:person) # => <ActionController::Parameters {"name"=>"Francesco"} permitted: false>
Otherwise raises ActionController::ParameterMissing:
ActionController::Parameters.new.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(person: "\t").require(:person) # 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
When given an array of keys, the method tries to require each one of them in order. If it succeeds, an array with the respective return values is returned:
params = ActionController::Parameters.new(user: { ... }, profile: { ... }) user_params, profile_params = params.require([:user, :profile])
Otherwise, the method re-raises the first exception found:
params = ActionController::Parameters.new(user: {}, profile: {}) user_params, profile_params = params.require([:user, :profile]) # ActionController::ParameterMissing: param is missing or the value is empty: user
Technically this method can be used to fetch terminal values:
# CAREFUL params = ActionController::Parameters.new(person: { name: "Finn" }) name = params.require(:person).require(:name) # CAREFUL
but take into account that at some point those ones have to be permitted:
def person_params params.require(:person).permit(:name).tap do |person_params| person_params.require(:name) # SAFER end end
for example.