如何在字段旁边显示错误消息
我有一个带有输入字段/标签等的表单。如何让错误消息显示在字段旁边?而不是在顶部聚集在一起?
我正在使用 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
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?
I am using devise, rails 3
I have this at the top of my form:
= 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想将错误消息放在文本字段的正下方,您可以这样做
What is
error_message_for
?-->嗯,这是一个漂亮的技巧,可以做一些很酷的事情
结果
错误后生成的标记
对应的 SCSS
How about this
if you want to put the error message just beneath the text field, you can do like this
What is
error_message_for
?--> Well, this is a beautiful hack to do some cool stuff
Result
Markup Generated after error
Corresponding SCSS
您可以使用 error_message_on
http://apidock.com/rails/ActionView/Helpers/ActiveRecordHelper/error_message_on
更新:
form.error_messages 已从 Rails 中删除,现在可以作为插件使用。请使用
rails plugin install git://github.com/rails/dynamic_form.git
安装它。You can use error_message_on
http://apidock.com/rails/ActionView/Helpers/ActiveRecordHelper/error_message_on
Update:
form.error_messages was removed from Rails and is now available as a plugin. Please install it with
rails plugin install git://github.com/rails/dynamic_form.git
.如果有人正在寻找一种方法来显示 Rails 6 中特定字段的错误消息:
If anyone is looking for a way how to display error messages for particular field in Rails 6:
您可以使用这个
也有用的链接:
You can use this
Also useful link:
只需在初始化程序文件夹中创建一个文件即可。
config/initializers/inline_errors.rb
将此代码放入其中:
PD:抱歉我的英语。
Just create a file in your initializers folder.
config/initializers/inline_errors.rb
Place this code in it:
PD: Sorry for my english.