From f801cd7959d92c565c3fb15f88ea59d9b49aa629 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 8 Jul 2006 05:35:08 +0000 Subject: 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 --- actionpack/CHANGELOG | 2 ++ .../lib/action_view/helpers/active_record_helper.rb | 16 +++++++++++++--- actionpack/test/template/active_record_helper_test.rb | 2 ++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index e9898276b3..68a02b01e5 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *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] diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb index c9d2c095bf..a7bfb77db3 100644 --- a/actionpack/lib/action_view/helpers/active_record_helper.rb +++ b/actionpack/lib/action_view/helpers/active_record_helper.rb @@ -120,13 +120,23 @@ module ActionView 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 diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb index ffaae99e28..928c31832a 100644 --- a/actionpack/test/template/active_record_helper_test.rb +++ b/actionpack/test/template/active_record_helper_test.rb @@ -152,6 +152,8 @@ class ActiveRecordHelperTest < Test::Unit::TestCase def test_error_for_block assert_dom_equal %(

1 error prohibited this post from being saved

There were problems with the following fields:

), error_messages_for("post") assert_equal %(

1 error prohibited this post from being saved

There were problems with the following fields:

), error_messages_for("post", :class => "errorDeathByClass", :id => "errorDeathById", :header_tag => "h1") + assert_equal %(

1 error prohibited this post from being saved

There were problems with the following fields:

), error_messages_for("post", :class => nil, :id => "errorDeathById", :header_tag => "h1") + assert_equal %(

1 error prohibited this post from being saved

There were problems with the following fields:

), error_messages_for("post", :class => "errorDeathByClass", :id => nil, :header_tag => "h1") end def test_error_messages_for_handles_nil -- cgit v1.2.3