锁定老帖子 主题:可定制的Rails错误回显
精华帖 (1) :: 良好帖 (13) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-09-16
最后修改:2009-09-16
Hooopo 写道 这只是汉化,但是还没有解决我想把email域的错误信息放在email文本框右边或下面的需求,而且这个需求也是完全正当的... 啦啦啦: <%= error_message_on @user, :email %> 如果出错就会产生: <div class="formError">伊妹儿不能为空</div> |
|
返回顶楼 | |
发表时间:2009-09-16
yuan 写道 我的想法一直是这样的:我打算直接不理这个验证错误的提示,反正前端有js挡着,就不用费功夫去搞这些了。model验证我认为主要是保证数据的正确性,而js的验证则是为了用户体验。对于那种能想办法绕过前端验证的家伙,就无所谓什么用户体验了吧。对于用web却禁掉js的家伙直接不予支持。
我觉得没有什么问题,大家认为如何? 1.去不去搞和能不能搞是两个概念,当然这是针对那些说rails封装过度的人说的.. 2.并不是所有禁用js的人都是要绕过你的js去做坏事,在浏览器里还有一个选项是“禁用js”之前,禁用js的用户就是合法用户。 3.有些设备不支持js,比如: 引用 JE改版后博客和圈子用了Ajax,貌似体验好了很多,但是我在用手机浏览的时候就无法回复。这一点《敏捷web开发》那本书做的就很好,在遇到不支持ajax的设备提供了普通的post方法,保证在所有设备上是可用的。
|
|
返回顶楼 | |
发表时间:2009-09-16
JavaEye的做法是自定义field_error_proc,配合前端的js和css,能够在后台出错的时候和前段校验有一样的效果:
ActionView::Base.field_error_proc = Proc.new {|html_tag, instance| %( #{html_tag} <script type="text/javascript"> var elm = $('#{instance.object_name}_#{instance.method_name}'); elm.tooltip = new Tooltip(elm, {backgroundColor: "#FC9", borderColor: "#C96", textColor: "#000", textShadowColor: "#FFF"}); elm.tooltip.content = "#{instance.error_message.kind_of?(Array) ? instance.error_message.join(',') : instance.error_message}" elm.addClassName('validation-failed'); try{elm.focus();}catch(e){}; </script> ) |
|
返回顶楼 | |
发表时间:2009-09-16
night_stalker 写道 Hooopo 写道 这只是汉化,但是还没有解决我想把email域的错误信息放在email文本框右边或下面的需求,而且这个需求也是完全正当的... 啦啦啦: <%= error_message_on @user, :email %> 如果出错就会产生: <div class="formError">伊妹儿不能为空</div> 居然这样也行,学习了.. 不过你这个方案(I18N+error_message_on+一堆配置,貌似还有判断locale和在view里加t)有点杀鸡用牛刀的感觉了。。 大多数时候人家只是要一个可以正确显示中文的页面.. |
|
返回顶楼 | |
发表时间:2009-09-16
最后修改:2009-09-16
直接用QuakeWang的ActionView::Base.field_error_proc就可以省你很多了,我自己业余的应用用这个的
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| if instance.error_message.kind_of?(Array) %(#{html_tag}<span class="validation_error"> #{instance.error_message.first}</span>) else %(#{html_tag}<span class="validation_error"> #{instance.error_message}</span>) end end |
|
返回顶楼 | |
发表时间:2009-09-16
最后修改:2009-09-16
Hooopo 写道 不过你这个方案(I18N+error_message_on+一堆配置,貌似还有判断locale和在view里加t)有点杀鸡用牛刀的感觉了。。 大多数时候人家只是要一个可以正确显示中文的页面.. i18n 是 rails 2.2 才集成的。不用白不用。 没几个网站会有多语言显示的奇怪要求的 …… 不用在 view 判断 locale, 直接在 environment.rb 中设 zh-cn 就行了。 activerecord 默认载入 en-us.yml (可以在 activerecord 的目录里找到),不改 locale,直接把 en-us.yml 的内容替换掉会更高效和省内存(可以省 50k !……) |
|
返回顶楼 | |
发表时间:2009-09-16
yearl 写道 这样分离显示的界面更友好。
同意 |
|
返回顶楼 | |
发表时间:2009-09-16
很明显,楼主没有去阅读过 rails文档
|
|
返回顶楼 | |
发表时间:2009-09-16
8.3 Customizing the Error Messages HTML
|
|
返回顶楼 | |
发表时间:2009-09-16
最后修改:2009-09-16
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也很不错。 |
|
返回顶楼 | |