| | 94 | |
|---|
| | 95 | |
|---|
| | 96 | |
|---|
| | 97 | |
|---|
| | 98 | # Returns a string with a div containing all of the error messages for the object and method located as instance variables by the names |
|---|
| | 99 | # given. This method mimics the output of +error_messages_for+, and is meant to be used in leau of +error_message_on+. |
|---|
| | 100 | # |
|---|
| | 101 | # This div can be tailored by the following options: |
|---|
| | 102 | # |
|---|
| | 103 | # * <tt>id</tt> - The id of the error div (default: errorExplanation) |
|---|
| | 104 | # * <tt>class</tt> - The class of the error div (default: errorExplanation) |
|---|
| | 105 | # * <tt>method_name</tt> - The object name to use in the header, or |
|---|
| | 106 | # any text that you prefer. If <tt>method_name</tt> is not set, the name of |
|---|
| | 107 | # the method will be used. |
|---|
| | 108 | # |
|---|
| | 109 | # Specifying one object: |
|---|
| | 110 | # |
|---|
| | 111 | # error_messages_on 'user', 'login' |
|---|
| | 112 | # |
|---|
| | 113 | # |
|---|
| | 114 | # NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what |
|---|
| | 115 | # you need is significantly different from the default presentation, it makes plenty of sense to access the object.errors |
|---|
| | 116 | # instance yourself and set it up. View the source of this method to see how easy it is. |
|---|
| | 117 | def error_messages_on(object, method, options = {}) |
|---|
| | 118 | html = {} |
|---|
| | 119 | options.stringify_keys! |
|---|
| | 120 | ['id', 'class'].each do |key| |
|---|
| | 121 | if options.include?(key) |
|---|
| | 122 | value = options[key] |
|---|
| | 123 | html[key] = value unless value.blank? |
|---|
| | 124 | else |
|---|
| | 125 | html[key] = 'errorExplanation' |
|---|
| | 126 | end |
|---|
| | 127 | end |
|---|
| | 128 | name = options['method_name'] ||= method.to_s.humanize.capitalize |
|---|
| | 129 | if (obj = instance_variable_get("@#{object}")) && (errors = obj.errors.on(method)) |
|---|
| | 130 | [errors].flatten! |
|---|
| | 131 | content_tag(:div, |
|---|
| | 132 | content_tag(:ul, errors.map {|msg| content_tag(:li, "#{name} #{msg}") }), |
|---|
| | 133 | html |
|---|
| | 134 | ) |
|---|
| | 135 | else |
|---|
| | 136 | '' |
|---|
| | 137 | end |
|---|
| | 138 | end |
|---|
| | 139 | |
|---|
| | 140 | |
|---|