Skip to content

SimplyMessages is a Rails plugin providing a unified flash notices and model error messages handling

License

Notifications You must be signed in to change notification settings

pjg/simply_messages

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simply_messages

simply_messages is a Rails plugin providing a unified notice/alert messages handling (like those: flash.now = ‘Yeah’) and model validation errors. Based on the message_block by Ben Hughes (github.com/railsgarden/message_block).

Requirements

Rails version 3.0+

Installation

Put this line in your Gemfile:

gem 'simply_messages'

Then bundle:

bundle

simply_messages uses standard I18n translations to display errors and as such requires at least the following keys to be present:

activerecord:
  errors:
    template:
      header:
        one: "1 error prohibited this %{model} from being saved"
        other: "%{count} errors prohibited this %{model} from being saved"

Usage

In ‘app/views/layouts/application.html.erb`:

<%= messages_block %>

By default the messages_block function will display all flash and flash messages and all errors for model object associated by name with the current controller (so it will display all model errors for @user model when ‘users’ is the current controller). It will also display appropriate messages for :success and :error flash keys.

We can also explicitly set the model we would like to display errors for, like this:

<%= messages_block :for => :comment %>

We can also display errors for more than one model:

<%= messages_block :for => [:post, :comment] %>

EXAMPLE 1

controller:

flash.now[:notice] = 'Post saved'

view:

<%= messages_block %>

will result in:

<div class="notice">
<p>Post saved</p>
</div>

EXAMPLE 2

Given a User model instance with one validation error:

user.errors
=> #<OrderedHash {:name=>["can't be blank"]}>

and this in the view in the users controller:

<%= messages_block %>

it will render:

<div class="alert">
<p>1 error prohibited this User from being saved:</p>
<ul>
<li>Name can't be blank</li>
</ul>
</div>

Contributing to simply_messages

  • Fork, fix, then send me a pull request.

Copyright © 2009-2011 Paweł Gościcki, released under the MIT license

About

SimplyMessages is a Rails plugin providing a unified flash notices and model error messages handling

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages