锁定老帖子 主题:可定制的Rails错误回显
精华帖 (1) :: 良好帖 (13) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-09-17
Hooopo 写道 amonlei 写道 很明显,楼主没有去阅读过 rails文档
文档都是用来查的..... 原来是error_message_on啊! 引用 error_message_on(object, method, *args) Returns a string containing the error message attached to the method on the object if one exists. This error message is wrapped in a DIV tag, which can be extended to include a :prepend_text and/or :append_text (to properly explain the error), and a :css_class to style it accordingly. object should either be the name of an instance variable or the actual object. The method can be passed in either as a string or a symbol. As an example, let’s say you have a model @post that has an error message on the title attribute: <%= error_message_on "post", "title" %> # => <div class="formError">can't be empty</div> <%= error_message_on @post, :title %> # => <div class="formError">can't be empty</div> <%= error_message_on "post", "title", :prepend_text => "Title simply ", :append_text => " (or it won't work).", :css_class => "inputError" %> <%= error_message_on "post", "title", :prepend_text => "Title simply ", :append_text => " (or it won't work).", :css_class => "inputError" %> 啊,土了,,原来error_message_on这个轮子已经造好了。 貌似ActionView::Base.field_error_proc也很不错。 文档不是用来查的,平时阅读以下,知道有哪些方面的内容,不然真是书到用时方恨少 |
|
返回顶楼 | |
发表时间:2009-09-20
amonlei 写道
8.3 Customizing the Error Messages HTML
非常棒!这样以后再写模型验证的时候,就可以把同一类型的写到一块了。 以前总是这么写 validates_numericality_of :weight, :message => "重量 必须是数字" validates_numericality_of :num, :message => "购买数量 必须是数字" 虽然验证的内容是一个类型的,但是因为字段名不一样,所以还得自己加上,现在可以全部加到一起了 validates_numericality_of :num, :weight, :message => "必须是数字" 反正错误信息会出现到表单元素的后面,方便很多,呵呵 |
|
返回顶楼 | |
发表时间:2009-09-23
最后修改:2009-09-23
class User < ActiveRecord::Base 效果如下:
不知,大家觉得如何? |
|
返回顶楼 | |