mirror of
https://github.com/github/rails.git
synced 2026-01-29 08:18:03 -05:00
Make error_message_on handle nil objects. [dan]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5799 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -85,8 +85,10 @@ module ActionView
|
||||
# <%= error_message_on "post", "title", "Title simply ", " (or it won't work)", "inputError" %> =>
|
||||
# <div class="inputError">Title simply can't be empty (or it won't work)</div>
|
||||
def error_message_on(object, method, prepend_text = "", append_text = "", css_class = "formError")
|
||||
if errors = instance_variable_get("@#{object}").errors.on(method)
|
||||
if object = instance_variable_get("@#{object}") && errors = object.errors.on(method)
|
||||
content_tag("div", "#{prepend_text}#{errors.is_a?(Array) ? errors.first : errors}#{append_text}", :class => css_class)
|
||||
else
|
||||
''
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -170,6 +170,10 @@ class ActiveRecordHelperTest < Test::Unit::TestCase
|
||||
def test_error_messages_for_handles_nil
|
||||
assert_equal "", error_messages_for("notthere")
|
||||
end
|
||||
|
||||
def test_error_message_on_handles_nil
|
||||
assert_equal "", error_message_on("notthere", "notthere")
|
||||
end
|
||||
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user