diff options
author | Guillermo Iguaran <guilleiguaran@gmail.com> | 2013-11-01 17:28:05 -0700 |
---|---|---|
committer | Guillermo Iguaran <guilleiguaran@gmail.com> | 2013-11-01 17:28:05 -0700 |
commit | c2b5a8e61ba0f35015e6ac949a5c8fce2042a1f2 (patch) | |
tree | e8ac76013d192271e786d749229799a84c74249e /actionpack/lib/action_controller | |
parent | 1918b12c0429caec2a6134ac5e5b42ade103fe90 (diff) | |
parent | b3f894c5282244b41221f98dfac5296cea5a4485 (diff) | |
download | rails-c2b5a8e61ba0f35015e6ac949a5c8fce2042a1f2.tar.gz rails-c2b5a8e61ba0f35015e6ac949a5c8fce2042a1f2.tar.bz2 rails-c2b5a8e61ba0f35015e6ac949a5c8fce2042a1f2.zip |
Merge pull request #9660 from sebasoga/change_strong_parameters_require_behaviour
Change ActionController::Parameters#require behavior when value is empty
Diffstat (limited to 'actionpack/lib/action_controller')
-rw-r--r-- | actionpack/lib/action_controller/metal/strong_parameters.rb | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index fcc76f6225..8ae7e474a3 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -10,8 +10,6 @@ module ActionController # params = ActionController::Parameters.new(a: {}) # params.fetch(:b) # # => ActionController::ParameterMissing: param not found: b - # params.require(:a) - # # => ActionController::ParameterMissing: param not found: a class ParameterMissing < KeyError attr_reader :param # :nodoc: @@ -21,6 +19,20 @@ module ActionController end end + # Raised when a required parameter value is empty. + # + # params = ActionController::Parameters.new(a: {}) + # params.require(:a) + # # => ActionController::EmptyParameter: value is empty for required key: a + class EmptyParameter < IndexError + attr_reader :param + + def initialize(param) + @param = param + super("value is empty for required key: #{param}") + end + end + # Raised when a supplied parameter is not expected. # # params = ActionController::Parameters.new(a: "123", b: "456") @@ -157,20 +169,22 @@ module ActionController self end - # Ensures that a parameter is present. If it's present, returns - # the parameter at the given +key+, otherwise raises an - # <tt>ActionController::ParameterMissing</tt> error. + # Ensures that a parameter is present. If it's present and not empty, + # returns the parameter at the given +key+, if it's empty raises + # an <tt>ActionController::EmptyParameter</tt> error, otherwise + # raises an <tt>ActionController::ParameterMissing</tt> error. # # ActionController::Parameters.new(person: { name: 'Francesco' }).require(:person) # # => {"name"=>"Francesco"} # - # ActionController::Parameters.new(person: nil).require(:person) - # # => ActionController::ParameterMissing: param not found: person - # # ActionController::Parameters.new(person: {}).require(:person) + # # => ActionController::EmptyParameter: value is empty for required key: person + # + # ActionController::Parameters.new(name: {}).require(:person) # # => ActionController::ParameterMissing: param not found: person def require(key) - self[key].presence || raise(ParameterMissing.new(key)) + raise(ActionController::ParameterMissing.new(key)) unless self.key?(key) + self[key].presence || raise(ActionController::EmptyParameter.new(key)) end # Alias of #require. |