Skip to content
This repository has been archived by the owner on Aug 17, 2017. It is now read-only.

Ensure that the key in require is valid. #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/action_controller/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/action_controller_required_params_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down