Fixes a bug where error_messages_for was returning an empty div [#4048 state:resolved] (ht: Geoff Garside)

This commit is contained in:
wycats 2010-03-27 03:09:33 -07:00
parent 418e5fc279
commit 0a352056a4
2 changed files with 5 additions and 1 deletions

View File

@ -127,7 +127,7 @@ module ActionView
object = convert_to_model(object) object = convert_to_model(object)
if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) && if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
(errors = obj.errors[method]) (errors = obj.errors[method]).presence
content_tag("div", content_tag("div",
"#{options[:prepend_text]}#{ERB::Util.h(errors.first)}#{options[:append_text]}".html_safe, "#{options[:prepend_text]}#{ERB::Util.h(errors.first)}#{options[:append_text]}".html_safe,
:class => options[:css_class] :class => options[:css_class]

View File

@ -266,6 +266,10 @@ class ActiveModelHelperTest < ActionView::TestCase
assert_dom_equal "<div class=\"differentError\">beforecan't be emptyafter</div>", error_message_on(:post, :author_name, :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after') assert_dom_equal "<div class=\"differentError\">beforecan't be emptyafter</div>", error_message_on(:post, :author_name, :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
end end
def test_error_message_on_handles_empty_errors
assert_equal "", error_message_on(@post, :tag)
end
def test_error_messages_for_many_objects def test_error_messages_for_many_objects
assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li><li>User email can't be empty</li></ul></div>), error_messages_for("post", "user") assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li><li>User email can't be empty</li></ul></div>), error_messages_for("post", "user")