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