Permalink
Browse files

Merge pull request #7314 from schneems/schneems/form_for_error

Check for Blank Record in form_for
  • Loading branch information...
2 parents feff27d + 60b650b commit a6e0d8cfcf483a88c9feda8a28b187043f5fab05 @rafaelfranca rafaelfranca committed Aug 11, 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
@@ -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
@@ -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 a6e0d8c

Please sign in to comment.