From 01cfda7cf280b5050b4431790702d63a956190c2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 14 Oct 2007 02:59:32 +0000 Subject: error_messages_for also takes :message and :header_message options which defaults to the old "There were problems with the following fields:" and " errors prohibited this from being saved". Closes #8270. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7870 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ .../action_view/helpers/active_record_helper.rb | 26 ++++++++++++---------- .../test/template/active_record_helper_test.rb | 8 +++++++ 3 files changed, 24 insertions(+), 12 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 8ba260dae6..97b5c03c69 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* error_messages_for also takes :message and :header_message options which defaults to the old "There were problems with the following fields:" and " errors prohibited this from being saved". #8270 [rmm5t, zach-inglis-lt3] + * Make sure that custom inflections are picked up by map.resources. #9815 [mislav] * Changed SanitizeHelper#sanitize to only allow the custom attributes and tags when specified in the call [DHH] diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb index e2e35cb0bd..a55c4966a3 100644 --- a/actionpack/lib/action_view/helpers/active_record_helper.rb +++ b/actionpack/lib/action_view/helpers/active_record_helper.rb @@ -106,11 +106,10 @@ module ActionView # * header_tag - Used for the header of the error div (default: h2) # * id - The id of the error div (default: errorExplanation) # * class - The class of the error div (default: errorExplanation) - # * object - The object (or array of objects) for which to display errors, - # if you need to escape the instance variable convention - # * object_name - The object name to use in the header, or - # any text that you prefer. If object_name is not set, the name of - # the first object will be used. + # * object - The object (or array of objects) for which to display errors, if you need to escape the instance variable convention + # * object_name - The object name to use in the header, or any text that you prefer. If object_name is not set, the name of the first object will be used. + # * header_message - The message in the header of the error div. Pass +nil+ or an empty string to avoid the header message altogether. (default: X errors prohibited this object from being saved) + # * message - The explanation message after the header message and before the error list. Pass +nil+ or an empty string to avoid the explanation message altogether. (default: There were problems with the following fields:) # # To specify the display for one object, you simply provide its name as a parameter. For example, for the +User+ model: # @@ -147,14 +146,17 @@ module ActionView html[key] = 'errorExplanation' end end - header_message = "#{pluralize(count, 'error')} prohibited this #{(options[:object_name] || params.first).to_s.gsub('_', ' ')} from being saved" + options[:object_name] ||= params.first + options[:header_message] = "#{pluralize(count, 'error')} prohibited this #{options[:object_name].to_s.gsub('_', ' ')} from being saved" unless options.include?(:header_message) + options[:message] ||= 'There were problems with the following fields:' unless options.include?(:message) 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), - html - ) + + contents = '' + contents << content_tag(options[:header_tag] || :h2, options[:header_message]) unless options[:header_message].blank? + contents << content_tag(:p, options[:message]) unless options[:message].blank? + contents << content_tag(:ul, error_messages) + + content_tag(:div, contents, html) else '' end diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb index 4630cbc1f3..6ef74aa8e7 100644 --- a/actionpack/test/template/active_record_helper_test.rb +++ b/actionpack/test/template/active_record_helper_test.rb @@ -207,6 +207,14 @@ class ActiveRecordHelperTest < Test::Unit::TestCase # should space object name assert_dom_equal %(

2 errors prohibited this chunky bacon from being saved

There were problems with the following fields:

  • User email can't be empty
  • Author name can't be empty
), error_messages_for(:user, :post, :object_name => "chunky_bacon") + + # hide header and explanation messages with nil or empty string + assert_dom_equal %(
  • User email can't be empty
  • Author name can't be empty
), error_messages_for(:user, :post, :header_message => nil, :message => "") + + # override header and explanation messages + header_message = "Yikes! Some errors" + message = "Please fix the following fields and resubmit:" + assert_dom_equal %(

#{header_message}

#{message}

  • User email can't be empty
  • Author name can't be empty
), error_messages_for(:user, :post, :header_message => header_message, :message => message) end def test_error_messages_for_non_instance_variable -- cgit v1.2.3