提交 f801cd79 编写于 作者: J Jeremy Kemper

Pass :id => nil or :class => nil to error_messages_for to supress that html...

Pass :id => nil or :class => nil to error_messages_for to supress that html attribute. Closes #3586.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4592 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 eede40bc
*SVN*
* Pass :id => nil or :class => nil to error_messages_for to supress that html attribute. #3586 [olivier_ansaldi@yahoo.com, sebastien@goetzilla.info]
* Reset @html_document between requests so assert_tag works. #4810 [jarkko@jlaine.net, easleydp@gmail.com]
* Update render :partial documentation. #5646 [matt@mattmargolis.net]
......
......@@ -120,13 +120,23 @@ def error_messages_for(*params)
objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
count = objects.inject(0) {|sum, object| sum + object.errors.count }
unless count.zero?
html = {}
[:id, :class].each do |key|
if options.include?(key)
value = options[key]
html[key] = value unless value.blank?
else
html[key] = 'errorExplanation'
end
end
header_message = "#{pluralize(count, 'error')} prohibited this #{(options[:object_name] || params.first).to_s.gsub('_', ' ')} from being saved"
error_messages = objects.map {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } }
content_tag(:div,
content_tag(options[:header_tag] || :h2, header_message) <<
content_tag(:p, 'There were problems with the following fields:') <<
content_tag(:ul, error_messages),
:id => options[:id] || 'errorExplanation', :class => options[:class] || 'errorExplanation')
content_tag(:p, 'There were problems with the following fields:') <<
content_tag(:ul, error_messages),
html
)
else
''
end
......
......@@ -152,6 +152,8 @@ def Post.content_columns() [ Column.new(:datetime, "written_on", "Written on") ]
def test_error_for_block
assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>1 error 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></ul></div>), error_messages_for("post")
assert_equal %(<div class="errorDeathByClass" id="errorDeathById"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => "errorDeathByClass", :id => "errorDeathById", :header_tag => "h1")
assert_equal %(<div id="errorDeathById"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => nil, :id => "errorDeathById", :header_tag => "h1")
assert_equal %(<div class="errorDeathByClass"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => "errorDeathByClass", :id => nil, :header_tag => "h1")
end
def test_error_messages_for_handles_nil
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册