百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

message_block

> 编程语言
开源

error_messages_for 的一个替代方案更加强大/灵活。

158 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

error_messages_for 的一个替代方案更加强大/灵活。

{}[http://travis-ci.org/rubiety/message_block] {}[https://gemnasium.com/rubiety/message_block] {}[https://codeclimate.com/github/rubiety/message_block]

= Message Block

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.

== Installation: Rails 3.1+ Asset Pipeline (message_block 2.0)

Include the gem using bundler in your Gemfile:

gem "message_block", "~> 2.0"

Include the stylesheet in your application.css:

/* *= require message_block */

== Installation: Rails 3.0 Without Asset Pipeline (message_block 1.1)

Include the gem using bundler in your Gemfile:

gem "message_block", "~> 1.1"

Then run the rake task to install the static files:

rake message_block:install

Then be sure to include the CSS definitions:

== 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. Then you can use the helper :

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: [:notice, :back, :confirm, :error, :alert, :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).

=== Example

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

[: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:

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.

== Running Tests

This gem uses appraisal to test with different versions of the dependencies. See Appraisal first for which versions are tested, then run to test all appraisals:

$ rake appraisal test

Issues· 12 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言