Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #15692 from sromano/falseClass

ActionController::Parameters#require now accepts FalseClass values

Conflicts:
	actionpack/test/controller/required_params_test.rb
  • Loading branch information...
commit 7545b27e70687d01b224b9c42d01a37a2028a21a 1 parent b9efc57
@matthewd matthewd authored
View
6 actionpack/CHANGELOG.md
@@ -1,3 +1,9 @@
+* ActionController::Parameters#require now accepts `false` values.
+
+ Fixes #15685.
+
+ *Sergio Romano*
+
* With authorization header `Authorization: Token token=`, `authenticate` now
recognize token as nil, instead of "token".
View
7 actionpack/lib/action_controller/metal/strong_parameters.rb
@@ -180,7 +180,12 @@ def permit!
# ActionController::Parameters.new(person: {}).require(:person)
# # => ActionController::ParameterMissing: param not found: person
def require(key)
- self[key].presence || raise(ParameterMissing.new(key))
+ value = self[key]
+ if value.present? || value == false
+ value
+ else
+ raise ParameterMissing.new(key)
+ end
end
# Alias of #require.
View
24 actionpack/test/controller/required_params_test.rb
@@ -24,4 +24,28 @@ class ActionControllerRequiredParamsTest < ActionController::TestCase
post :create, { book: { name: "Mjallo!" } }
assert_response :ok
end
+
+ test "required parameters with false value will not raise" do
+ post :create, { book: { name: false } }
+ assert_response :ok
+ end
+end
+
+class ParametersRequireTest < ActiveSupport::TestCase
+
+ test "required parameters should accept and return false value" do
+ assert_equal(false, ActionController::Parameters.new(person: false).require(:person))
+ end
+
+ test "required parameters must not be nil" do
+ assert_raises(ActionController::ParameterMissing) do
+ ActionController::Parameters.new(person: nil).require(:person)
+ end
+ end
+
+ test "required parameters must not be empty" do
+ assert_raises(ActionController::ParameterMissing) do
+ ActionController::Parameters.new(person: {}).require(:person)
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.