From 2fd54223ccf4c0ff1a8e02de8765bc500099dde2 Mon Sep 17 00:00:00 2001 From: Bryan Rite Date: Thu, 30 May 2013 13:10:53 -0700 Subject: [PATCH] Ensure that the key in require is valid. --- lib/action_controller/parameters.rb | 6 +++++- test/action_controller_required_params_test.rb | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/action_controller/parameters.rb b/lib/action_controller/parameters.rb index 644f5e6..8488e86 100644 --- a/lib/action_controller/parameters.rb +++ b/lib/action_controller/parameters.rb @@ -52,7 +52,11 @@ def permit! end def require(key) - self[key].presence || raise(ActionController::ParameterMissing.new(key)) + if self[key].present? && (self[key].is_a?(ActionController::Parameters) || permitted_scalar?(self[key])) + self[key] + else + raise(ActionController::ParameterMissing.new(key)) + end end alias :required :require diff --git a/test/action_controller_required_params_test.rb b/test/action_controller_required_params_test.rb index 5950f93..a6eec62 100644 --- a/test/action_controller_required_params_test.rb +++ b/test/action_controller_required_params_test.rb @@ -18,6 +18,11 @@ class ActionControllerRequiredParamsTest < ActionController::TestCase assert_response :bad_request end + test "required parameters in wrong format will raise exception" do + post :create, { :book => [ { :title => "Mjallo!" } ] } + assert_response :bad_request + end + test "required parameters that are present will not raise" do post :create, { :book => { :name => "Mjallo!" } } assert_response :ok