Ticket #8270: more_options_on_error_messages_for_with_tests.diff
| File more_options_on_error_messages_for_with_tests.diff, 4.2 kB (added by rmm5t, 1 year ago) |
|---|
-
test/template/active_record_helper_test.rb
old new 202 202 203 203 # should space object name 204 204 assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this chunky bacon from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => "chunky_bacon") 205 206 # hide header and explanation messages with nil or empty string 207 assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :header_message => nil, :message => "") 208 209 # override header and explanation messages 210 header_message = "Yikes! Some errors" 211 message = "Please fix the following fields and resubmit:" 212 assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>#{header_message}</h2><p>#{message}</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :header_message => header_message, :message => message) 205 213 end 206 214 207 215 def test_form_with_string_multipart -
lib/action_view/helpers/active_record_helper.rb
old new 101 101 # * <tt>header_tag</tt> - Used for the header of the error div (default: h2) 102 102 # * <tt>id</tt> - The id of the error div (default: errorExplanation) 103 103 # * <tt>class</tt> - The class of the error div (default: errorExplanation) 104 # * <tt>object_name</tt> - The object name to use in the header, or 105 # any text that you prefer. If <tt>object_name</tt> is not set, the name of106 # the first object will be used.104 # * <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. 105 # * <tt>header_message</tt> - 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) 106 # * <tt>message</tt> - 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:) 107 107 # 108 108 # To specify the display for one object, you simply provide its name as a parameter. For example, for the +User+ model: 109 109 # … … 131 131 html[key] = 'errorExplanation' 132 132 end 133 133 end 134 header_message = "#{pluralize(count, 'error')} prohibited this #{(options[:object_name] || params.first).to_s.gsub('_', ' ')} from being saved" 134 options[:object_name] ||= params.first 135 options[:header_message] = "#{pluralize(count, 'error')} prohibited this #{options[:object_name].to_s.gsub('_', ' ')} from being saved" unless options.include?(:header_message) 136 options[:message] ||= 'There were problems with the following fields:' unless options.include?(:message) 137 135 138 error_messages = objects.map {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } } 136 content _tag(:div,137 content_tag(options[:header_tag] || :h2, header_message) <<138 content_tag(:p, 'There were problems with the following fields:') <<139 content_tag(:ul, error_messages),140 html141 )139 contents = '' 140 contents << content_tag(options[:header_tag] || :h2, options[:header_message]) unless options[:header_message].blank? 141 contents << content_tag(:p, options[:message]) unless options[:message].blank? 142 contents << content_tag(:ul, error_messages) 143 144 content_tag(:div, contents, html) 142 145 else 143 146 '' 144 147 end