diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-01 13:59:16 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-01 13:59:16 +0000 |
commit | 005371e16cf214509bb988dcf5d0eef3ee23157d (patch) | |
tree | 92cd3758cac134e2a82537d87724f9ca80e4d733 /actionpack/lib | |
parent | 0daa29ece29191b288fe86d3616bea0357325419 (diff) | |
download | rails-005371e16cf214509bb988dcf5d0eef3ee23157d.tar.gz rails-005371e16cf214509bb988dcf5d0eef3ee23157d.tar.bz2 rails-005371e16cf214509bb988dcf5d0eef3ee23157d.zip |
Added options to tailor header tag, div id, and div class on ActiveRecordHelper#error_messages_for [josh]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@41 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_view/helpers/active_record_helper.rb | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb index b02b807fe1..de18696992 100644 --- a/actionpack/lib/action_view/helpers/active_record_helper.rb +++ b/actionpack/lib/action_view/helpers/active_record_helper.rb @@ -76,14 +76,24 @@ module ActionView end end - def error_messages_for(object_name) - object = instance_eval("@#{object_name}") + # 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: + # + # ::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) + def error_messages_for(object_name, options={}) + object = instance_eval "@#{object_name}" unless object.errors.empty? - "<div id=\"errorExplanation\">" + - "<h2>#{object.errors.count} error#{"s" unless object.errors.count == 1} prohibited this #{object_name.gsub("_", " ")} from being saved</h2>" + - "<p>There were problems with the following fields (marked in red below):</p>" + - "<ul>#{object.errors.full_messages.collect { |msg| "<li>#{msg}</li>"}}</ul>" + - "</div>" + content_tag("div", + content_tag( + options[:header_tag] || "h2", + "#{pluralize(object.errors.count, "error")} prohibited this #{object_name.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" + ) end end |