aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
authorMarcel Molina <marcel@vernix.org>2006-04-26 23:09:08 +0000
committerMarcel Molina <marcel@vernix.org>2006-04-26 23:09:08 +0000
commit43ee8ab6e2aa1efeb848885ed51f14709990cc08 (patch)
tree1cff111083576260cbdecb19c2b84eb9a3dffeed /actionpack/lib
parent8ee378f3c46c164cf8d7559bbb6a65ce1bb786fb (diff)
downloadrails-43ee8ab6e2aa1efeb848885ed51f14709990cc08.tar.gz
rails-43ee8ab6e2aa1efeb848885ed51f14709990cc08.tar.bz2
rails-43ee8ab6e2aa1efeb848885ed51f14709990cc08.zip
Allow error_messages_for to report errors for multiple objects, as well as support for customizing the name of the object in the error summary header. Closes #4186. [andrew@redlinesoftware.com, Marcel Molina Jr.]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4287 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view/helpers/active_record_helper.rb46
1 files changed, 30 insertions, 16 deletions
diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb
index 828aebf790..c9d2c095bf 100644
--- a/actionpack/lib/action_view/helpers/active_record_helper.rb
+++ b/actionpack/lib/action_view/helpers/active_record_helper.rb
@@ -90,31 +90,45 @@ module ActionView
end
end
- # Returns a string with a div containing all the error messages for the object located as an instance variable by the name
- # of <tt>object_name</tt>. This div can be tailored by the following options:
+ # Returns a string with a div containing all of the error messages for the objects located as instance variables by the names
+ # given. If more than one object is specified, the errors for the objects are displayed in the order that the object names are
+ # provided.
+ #
+ # This div can be tailored by the following options:
#
# * <tt>header_tag</tt> - Used for the header of the error div (default: h2)
# * <tt>id</tt> - The id of the error div (default: errorExplanation)
# * <tt>class</tt> - The class of the error div (default: errorExplanation)
+ # * <tt>object_name</tt> - The object name to use in the header, or
+ # any text that you prefer. If <tt>object_name</tt> is not set, the name of
+ # the first object will be used.
+ #
+ # Specifying one object:
+ #
+ # error_messages_for 'user'
+ #
+ # Specifying more than one object (and using the name 'user' in the
+ # header as the <tt>object_name</tt> instead of 'user_common'):
+ #
+ # error_messages_for 'user_common', 'user', :object_name => 'user'
#
# NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what
# you need is significantly different from the default presentation, it makes plenty of sense to access the object.errors
# instance yourself and set it up. View the source of this method to see how easy it is.
- def error_messages_for(object_name, options = {})
- options = options.symbolize_keys
- object = instance_variable_get("@#{object_name}")
- if object && !object.errors.empty?
- content_tag("div",
- content_tag(
- options[:header_tag] || "h2",
- "#{pluralize(object.errors.count, "error")} prohibited this #{object_name.to_s.gsub("_", " ")} from being saved"
- ) +
- content_tag("p", "There were problems with the following fields:") +
- content_tag("ul", object.errors.full_messages.collect { |msg| content_tag("li", msg) }),
- "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
- )
+ def error_messages_for(*params)
+ options = params.last.is_a?(Hash) ? params.pop.symbolize_keys : {}
+ objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
+ count = objects.inject(0) {|sum, object| sum + object.errors.count }
+ unless count.zero?
+ 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')
else
- ""
+ ''
end
end