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 gem comes with an example stylesheet and images.
This gem is testing working with Rails 3.0 only!
Include the gem using bundler in your Gemfile:
Then run the rake task to install the static files:
Then be sure to include the CSS definitions:
<%= stylesheet_include_tag "message_block" %>
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. Then you can use the helper <%= message_block %>:
The first argument specifies a hash options:
:on - specifies one or many model names for which to check error messages. You can specify the special value of :all which will grab all view assignments that contain an ActiveModel “errors” method.
: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.
:container - specifies which block-level element to contain the errors (defaults to :div).
Imagine you have a form for entering a user and a comment:
<%= message_block :on => [: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" class="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>
Note that instead of manually specifying models you wish to report errors on, you can instead use the special :all value which will automatically use all view assign values that contain an ActiveModel::Errors “errors” method.