Skip to content
Browse files

check for nil or empty record in form_for

if nil or an empty array is passed into form_for you get a horrible error message, this one is much more indicative of what the programmer needs to know to fix the problem.
  • Loading branch information...
1 parent 581a927 commit 60b650b18c82a050ab81f924e23f7d308ffd8972 @schneems schneems committed Aug 9, 2012
Showing with 15 additions and 0 deletions.
  1. +1 −0 actionpack/lib/action_view/helpers/form_helper.rb
  2. +14 −0 actionpack/test/template/form_helper_test.rb
View
1 actionpack/lib/action_view/helpers/form_helper.rb
@@ -423,6 +423,7 @@ def form_for(record, options = {}, &proc)
object = nil
else
object = record.is_a?(Array) ? record.last : record
+ raise ArgumentError, "First argument in form cannot contain nil or be empty" if object.blank?
object_name = options[:as] || model_name_from_record_or_class(object).param_key
apply_form_for_options!(record, object, options)
end
View
14 actionpack/test/template/form_helper_test.rb
@@ -1045,6 +1045,20 @@ def test_form_for_requires_block
end
end
+ def test_form_for_requires_arguments
+ error = assert_raises(ArgumentError) do
+ form_for(nil, :html => { :id => 'create-post' }) do
+ end
+ end
+ assert_equal "First argument in form cannot contain nil or be empty", error.message
+
+ error = assert_raises(ArgumentError) do
+ form_for([nil, nil], :html => { :id => 'create-post' }) do
+ end
+ end
+ assert_equal "First argument in form cannot contain nil or be empty", error.message
+ end
+
def test_form_for
form_for(@post, :html => { :id => 'create-post' }) do |f|
concat f.label(:title) { "The Title" }

0 comments on commit 60b650b

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