Skip to content
Browse files

Merge pull request #15692 from sromano/falseClass

ActionController::Parameters#require now accepts FalseClass values
  • Loading branch information...
2 parents 49fee3d + 540d153 commit 497def80b53db73712a6abf2c066e9bc4980ccd2 @matthewd matthewd committed Jun 14, 2014
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
@@ -184,7 +184,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
18 actionpack/test/controller/required_params_test.rb
@@ -24,10 +24,26 @@ 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 must be present not merely not nil" do
+
+ 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

0 comments on commit 497def8

Please sign in to comment.
Something went wrong with that request. Please try again.