Return to Snippet

Revision: 35029
at November 1, 2010 10:33 by dre1080


Updated Code
# Andrew Wayne 2010
# Error messages for Ruby on Rails 3
# This isnt available in Rails 3 and is a custom module
# made to work like the previous Rails


# Put this in your ApplicationHelper or create an error_messages_helper.rb file
# in your helpers dir

#------------- START --------------#

module ErrorMessagesHelper
# Render error messages for the given objects. 
# The :message and :header_message options are allowed.
  def error_messages_for(*objects)
    messages = objects.compact.map { |o| o.errors.full_messages }.flatten
    unless messages.empty?
      content_tag(:div, :class => "error_messages") do
        list_items = messages.map { |msg| content_tag(:li, msg) }
        content_tag(:ul, list_items.join.html_safe)
      end
    end
  end

  module FormBuilderExtensions
    def error_messages(options = {})
      @template.error_messages_for(@object, options)
    end
  end
end

ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderExtensions)

# -------------------------------- END ---------------------------------------------- #


# Example Usage

# in UsersController
@user = User.new(params[:user])

# in Views
<%= error_messages_for (@user) %>

Revision: 35028
at November 1, 2010 10:32 by dre1080


Initial Code
# Andrew Wayne 2010
# Error messages for Rails 3
# This isnt available in Rails 3 and is a custom module
# made to work like the previous Rails


# Put this in your ApplicationHelper or create an error_messages_helper.rb file
# in your helpers dir

#------------- START --------------#

module ErrorMessagesHelper
# Render error messages for the given objects. 
# The :message and :header_message options are allowed.
  def error_messages_for(*objects)
    messages = objects.compact.map { |o| o.errors.full_messages }.flatten
    unless messages.empty?
      content_tag(:div, :class => "error_messages") do
        list_items = messages.map { |msg| content_tag(:li, msg) }
        content_tag(:ul, list_items.join.html_safe)
      end
    end
  end

  module FormBuilderExtensions
    def error_messages(options = {})
      @template.error_messages_for(@object, options)
    end
  end
end

ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderExtensions)

# -------------------------------- END ---------------------------------------------- #


# Example Usage

# in UsersController
@user = User.new(params[:user])

# in Views
<%= error_messages_for (@user) %>

Initial URL

                                

Initial Description

                                

Initial Title
Error Messages for Rails 3

Initial Tags

                                

Initial Language
Rails