Commit
Normal use is unchanged. When used with multiple parameters the error raised in case a parameter is missing lists all the missing parameters. Returns the params hash if no required parameters are missing.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,16 @@ This declaration whitelists the `name`, `emails` and `friends` attributes. It is | |
|
||
Thanks to Nick Kallen for the permit idea! | ||
|
||
## Requiring multiple values | ||
|
||
In some cases you might want to require several parameters to be present before proceeding, such as for an API where the params might be a flat hash. You can use `require` to do this: | ||
|
||
``` ruby | ||
params.require(:username, :password).permit(:version) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dhh
Member
|
||
``` | ||
|
||
When used with multiple inputs `require` raises a `ActionController::MissingParameter` error with *all* the missing attributes specified in the message. If no values are missing the params hash is returned unmodified. | ||
|
||
## Handling of Unpermitted Keys | ||
|
||
By default parameter keys that are not explicitly permitted will be logged in the development and test environment. In other environments these parameters will simply be filtered out and ignored. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,29 @@ class ParametersRequireTest < ActiveSupport::TestCase | |
ActionController::Parameters.new(:person => {}).require(:person) | ||
end | ||
end | ||
|
||
test "permit multiple required parameters" do | ||
params = ActionController::Parameters.new(:username => 'user', :password => '<3<3<3<3') | ||
assert_nothing_raised(ActionController::ParameterMissing) do | ||
params.require(:username, :password) | ||
end | ||
|
||
assert params.has_key?(:username) | ||
assert params.has_key?(:password) | ||
end | ||
|
||
test "multiple required parameters must be present not merely not nil" do | ||
params = ActionController::Parameters.new(:username => '', :password => nil) | ||
assert_raises(ActionController::ParameterMissing) do | ||
params.require(:username, :password) | ||
end | ||
end | ||
|
||
test "all parameters are returned after required with multiple parameters" do | ||
params = ActionController::Parameters.new(:username => 'user', :password => '<3<3<3<3', :version => 1) | ||
|
||
params.require(:username, :password) | ||
|
||
assert params.has_key?(:version) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
d-Pixie
via email
Contributor
|
||
end | ||
end |
My understanding of the require / permit syntax was that, if you do something like
params.require(:post).permit(:title, :body)
, that the:title
and:body
params were permitted only within the:post
key, e.g.{post: {title: 'Title', body: 'Body'}
. It seems like your example here assumes that theversion
param will be passed along as a top-level param, along withusername
andpassword
. Am I misunderstanding the API? Thanks.