instance method error_message_on

Ruby on Rails 2.3.18

Since v2.2.3 Last seen in v2.3.18

Available in: v2.2.3 v2.3.18

Signature

error_message_on(object, method, *args)

Returns a string containing the error message attached to the method on the object if one exists. This error message is wrapped in a DIV tag, which can be extended to include a :prepend_text and/or :append_text (to properly explain the error), and a :css_class to style it accordingly. object should either be the name of an instance variable or the actual object. The method can be passed in either as a string or a symbol. As an example, let’s say you have a model @post that has an error message on the title attribute:

<%= error_message_on "post", "title" %>
# => <div class="formError">can't be empty</div>

<%= error_message_on @post, :title %>
# => <div class="formError">can't be empty</div>

<%= error_message_on "post", "title",
    :prepend_text => "Title simply ",
    :append_text => " (or it won't work).",
    :css_class => "inputError" %>

Parameters

object req
method req
args rest
Source
# File actionpack/lib/action_view/helpers/active_record_helper.rb, line 109
      def error_message_on(object, method, *args)
        options = args.extract_options!
        unless args.empty?
          ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate ' +
            'prepend_text, append_text, and css_class arguments', caller)

          options[:prepend_text] = args[0] || ''
          options[:append_text] = args[1] || ''
          options[:css_class] = args[2] || 'formError'
        end
        options.reverse_merge!(:prepend_text => '', :append_text => '', :css_class => 'formError')

        if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
          (errors = obj.errors.on(method))
          content_tag("div",
            "#{options[:prepend_text]}#{ERB::Util.html_escape(errors.is_a?(Array) ? errors.first : errors)}#{options[:append_text]}".html_safe,
            :class => options[:css_class]
          )
        else
          ''
        end
      end

Defined in actionpack/lib/action_view/helpers/active_record_helper.rb line 109 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Helpers::ActiveRecordHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close