如何在字段旁边显示错误消息 [英] How to Show Error Messages Next to Field

查看:22
本文介绍了如何在字段旁边显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含输入字段/标签等的表单.如何让错误消息显示在该字段旁边?而不是在顶部聚集在一起?

I have a form with input fields/labels etc. How do I get the error message to show up next to the field? instead of clumped together at the top?

我正在使用设计,导轨 3

I am using devise, rails 3

我的表单顶部有这个:

 = form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
- if resource.errors.any?
  #errorExplanation
    %h2
      = pluralize(resource.errors.count, "error")
      prevented this user from being saved:
    %ul
      - resource.errors.full_messages.each do |msg|
        %li
          = msg

推荐答案

你可以使用这个

- if @resource.errors[:field_name]
  ...

同样有用的链接:

http://guides.rubyonrails.org/active_record_validations.html#working-with-validation-错误

这篇关于如何在字段旁边显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆