Top Level Namespace
Instance Method Summary collapse
-
#error_messages_for(*params) ⇒ Object
Disclaimer - Code is copied as is from rails 2.3.10, and is only intended to act as a polyfill for error_messages_for() usage In projects migrating from old rails env –@avnerner ([email protected]) Returns a string with a
DIV
containing all of the error messages for the objects located as instance variables by the names given.
Instance Method Details
#error_messages_for(*params) ⇒ Object
Disclaimer - Code is copied as is from rails 2.3.10, and is only intended to act as a polyfill for error_messages_for() usage In projects migrating from old rails env –@avnerner ([email protected]) 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:
-
: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. -
:header_message
- The message in the header of the error div. Passnil
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. Passnil
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:
'user'
To specify more than one object, you simply list them; optionally, you can add an extra :object_name
parameter, which will be the name used in the header message:
'user_common', 'user', :object_name => 'user'
If the objects cannot be located as instance variables, you can add an extra :object
parameter which gives the actual object (or array of objects to use):
'user', :object => @question.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.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/errors_for.rb', line 42 def (*params) = params..symbolize_keys if object = .delete(:object) objects = Array.wrap(object) else objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact end count = objects.inject(0) {|sum, object| sum + object.errors.count } unless count.zero? html = {} [:id, :class].each do |key| if .include?(key) value = [key] html[key] = value unless value.blank? else html[key] = 'errorExplanation' end end [:object_name] ||= params.first I18n. :locale => [:locale], :scope => [:activerecord, :errors, :template] do |locale| = if .include?(:header_message) [:header_message] else object_name = [:object_name].to_s object_name = I18n.t(object_name, :default => object_name.gsub('_', ' '), :scope => [:activerecord, :models], :count => 1) locale.t :header, :count => count, :model => object_name end = .include?(:message) ? [:message] : locale.t(:body) = objects.sum {|object| object.errors..map {|msg| content_tag(:li, ERB::Util.html_escape(msg)) } }.join.html_safe contents = '' contents << content_tag([:header_tag] || :h2, ) unless .blank? contents << content_tag(:p, ) unless .blank? contents << content_tag(:ul, ) content_tag(:div, contents.html_safe, html) end else '' end end |