26
Nov
08

message_block: a error_messages_for replacement for flash message and model error handling

Message Block Example

One of the most common needs in any application I build is to have some abstract way of handling messages to end users.  Sometimes I’ll want to show a confirmation message or a warning.  Other times I’ll want to show a confirmation message but also show ActiveRecord validations.  While the error_messages_for helper in Rails works fairly well for showing ActiveRecord validation issues, I wanted a unified approach to handling this and flash messaging with multiple flash types in one package.

I blogged before about an approach I developed to solve this problem, I rolled my own message_block plugin.  The README file explains things pretty so be sure to check it out for more details, but here is the intro:

Introduction:

Implements the common view pattern by which a list of messages are shown at the top, often a combination of flash messages and ActiveRecord validation issues on one or more models. This allows for a nice, stylized block of messages at the top of the page with icons indicating what type of message it is (error, confirmation, warning, etc.)

This view helper acts as a replacement for error_messages_for by taking error messages from your models and combing them with flash messages (multiple types such as error, confirm, etc.) and outputting them to your view. This plugin comes with an example stylesheet and images.

Usage:

Once you install this, you should now have a set of images at public/images/message_block and a basic stylesheet installed at public/stylesheets/message_block.css. First you’ll want to either reference this in your layout or copy the declarations to your main layout. Then you can use the helper <%= message_block %> as described below:

The first argument specifies a hash options:

  • :o n – specifies one or many model names for which to check error messages.
  • :model_error_type – specifies the message type to use for validation errors; defaults to ‘error’
  • :flash_types – specifies the keys to check in the flash hash. Messages will be grouped in ul lists according to this type. Defaults to: %w(back confirm error info warn)
  • :html – Specifies HTML options for the containing div
  • :id – Specifies ID of the containing div; defaults to ‘message_block’
  • :class – Specifies class name of the containing div; defaults to nothing.

Imagine you have a form for entering a user and a comment:

<%= message_block :o n => [:user, :comment] %>

Imagine also you set these flash variables in the controller:

  class CommentsController
    def create
      flash.now[:error] = "Error A"
      flash.now[:confirm] = "Confirmation A"  # Note you can use different types
      flash.now[:warn] = ["Warn A", "Warn B"]  # Can set to an array for multiple messages
    end
  end

And let’s say that you want to show these messages but also show the validation issues given that both user and comment fail ActiveRecord validation:

  <div id="message_block">
    <ul class="error">
      <li>Error A</li>
      <li>User first name is required.</li>
      <li>Comment contents is required.</li>
    </ul>
    <ul class="confirm">
      <li>Confirmation A</li>
    </ul>
    <ul class="warn">
      <li>Warn A</li>
      <li>Warn B</li>
    </ul>
  </div>

Which will by default leave you with this look:

Message Block Example

message_block on GitHub


4 Responses to “message_block: a error_messages_for replacement for flash message and model error handling”


  1. 1 sgarza Jan 11th, 2009 at 4:12 am

    This is so cool!

    Thanks

    Im following you on github now.

    thanks again.

  2. 2 Travis Jan 15th, 2009 at 1:33 pm

    Thank you very much for this plugin. Is there any way to add the error header messages like the standard error_messages_for method? I couldn’t find a way, so at the moment I’m just checking for errors.empty? in the model objects I’m validating, and then placing the headers before message_block – that’s fine but seems a little clumsy!

  3. 3 jerik Jan 20th, 2009 at 9:24 pm

    Hi,

    installed message_block and tried it. Currently it throws the following error:
    “undefined method `errors’ for #”

    What I did:
    1. Installed it via environment.rb
    2. Added into my app/view/layout/application.html.erb, besides the link to the message_block.css:
    [:foos] %>
    3. In app/controllers/foos_controller.rb
    def startup
    flash.now[:confirm] = “Confirmation A” # for testing
    end
    4. run the server and launched the application, which resulted in the described error.

    Any idea?

    Cheers — jerik

  4. 4 FF Nov 18th, 2009 at 11:16 pm

    Great job, thanks for sharing it.
    Can you write a detailed way to install the plugin ?
    You began the “usage” section with “Once you install this…”, but the newbies (like me) be one basic step behing….The installation of the helper method.
    Thanks in advance…
    FF

Leave a Reply




  • Ben Hughes

    I'm a freelance developer working with Ruby and other modern tools to build web applications, based currently out of Rochester, NY. I love to learn about new technologies and am always trying to achieve elegance and beauty through code.

    When I'm not writing software, I like to play tennis, dabble in jazz piano, and ponder economics. I'm a big fan of: world travel and cultures, jazz music, Korean food, coffee, and having interesting conversations.

  • Recommend Me
November 2008
M T W T F S S
« Jul   Dec »
 12
3456789
10111213141516
17181920212223
24252627282930