Topic: errors.full_messages, errors for one field in a single line

include this as a helper to get the following..

instead of having all errors listed in a big block i.e. one item per line (see below)
Name can't be blank
Name must be 5 characters
Email is not valid
Email isnt the required format
Email is too short

I thought it was easier to read them as a user for each field on single line

Name can't be blank & must be 5 characters
Email is not valid & isnt the required format & is too short

so I include this as a helper file

module ErrorsHelper
     def error_messages_for(*params)
        options = params.extract_options!.symbolize_keys

        objects = Array.wrap(options.delete(:object) || params).map do |object|
          object = instance_variable_get("@#{object}") unless object.respond_to?(:to_model)
          object = convert_to_model(object)

          if object.class.respond_to?(:model_name)
            options[:object_name] ||= object.class.model_name.human.downcase
          end

          object
        end

        objects.compact!
        count = objects.inject(0) {|sum, object| sum + object.errors.count }

        unless count.zero?
          html = {}
          [:id, :class].each do |key|
            if options.include?(key)
              value = options[key]
              html[key] = value unless value.blank?
            else
              html[key] = 'errorExplanation'
            end
          end
          options[:object_name] ||= params.first

          I18n.with_options :locale => options[:locale], :scope => [:errors, :template] do |locale|
            header_message = if options.include?(:header_message)
              options[:header_message]


           options[:header_message]
            else
              locale.t :header, :count => count, :model => options[:object_name].to_s.gsub('_', ' ')
            end

            message = options.include?(:message) ? options[:message] : locale.t(:body)




            error_messages = objects.sum do |object|
              object.errors.on(:name)
              full_flat_messages(object).map do |msg|
                  content_tag(:li, ERB::Util.html_escape(msg))
              end
            end.join
           
           
            contents = ''
            contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
            contents << content_tag(:p, message) unless message.blank?
            contents << content_tag(:ul, error_messages)

            content_tag(:div, contents, html)
          end
        else
          ''
        end
    end

####################
#
# added to make the errors display in a single line per field
#
####################
     def full_flat_messages(object)
       full_messages = []

       object.errors.each_key do |attr|
        msg_part=msg=''
        object.errors[attr].each do |message|
           next unless message
           if attr == "base"
             full_messages << message
           else
             msg=object.class.human_attribute_name(attr)
             msg_part+= I18n.t('activerecord.errors.format.separator', :default => ' ') + (msg_part=="" ? '': ' & ' ) + message
           end
         end
         full_messages << "#{msg} #{msg_part}" if msg!=""
       end
       full_messages
     end

end


I just thought it useful for others as I ended up having lots of validations for my forms and the errors were consuming the entire page. Now it only takes up a few lines of HTML to output the errors.

any ideas to make this better, feel free to improve :-)

Bert

Re: errors.full_messages, errors for one field in a single line

Thanks )) it  was very usefull)) Highly appreciate your desire to help!! Cheers up))I looked around on the forum, but couldn't really find anything about this, so maybe this isn't a problem for many people.

Last edited by Dominique24 (2012-06-14 02:58:46)

Re: errors.full_messages, errors for one field in a single line

It's just gonna get worse when apps are on the Android market that are free and filled with viruses! Rails and email are particularly daunting. Does anyone recommend I try and install some kind of email code so that I can track emails better? A third-party app, for instance? Or should I hire a company to handle my scanpst support?

Last edited by charlesperez (2012-01-03 14:43:53)

Re: errors.full_messages, errors for one field in a single line

How can I get this same functionality in rails 2.3.15??