在顶部 <div> 中显示 simple_form 错误消息
[英] Displaying simple_form error messages in top <div>
本文介绍了在顶部 <div> 中显示 simple_form 错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下两个_forms:
用户表单
<%= simple_form_for(@user, :url => @target) do |f|%><% if @user.errors.any?%><div id="error_explanation"><h2><%=pluralize(@user.errors.count, "error") %>禁止保存此用户:</h2><ul><% @user.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>
<%结束%><%= f.input :email, :label =>用户电子邮件"%><%= f.input :password, :label =>用户密码"%><%= f.input :first_name %><%= f.input :last_name %><%= f.button :submit %><%结束%>
租户表格
<%= simple_form_for(@tenant, :url => @target) do |f|%><% if @tenant.errors.any?%><div id="error_explanation"><h2><%=pluralize(@tenant.errors.count, "error") %>禁止保存此租户:</h2><ul><% @tenant.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>
<%结束%><%= f.input :name, :label =>'名称', :required =>真%><%= f.input :billing_email, :label =>'电子邮件', :required =>真%><%= f.input :country, :label =>'国家', :required =>真%><%= f.button :submit %><%结束%>
我从 stackoverflow 看到以下帖子 Rails 3.0 中的 f.error_messages
这里有一个方法,可以通过使用 f.error_messages 从简单的表单返回错误消息,但我一直无法让它工作,因为我不确定这个方法应该保存在哪里.有人有任何提示吗?方法如下:
class StandardBuilder "title-error")错误列表< "error-recap round-border")结尾结尾
解决方案
只需将 error: false 添加到您的输入中,这不会清除 css,但会清除内联错误
f.input error: false 或 :error =>错误的
来自 http://ruby.railstutorial.org/book/ruby-on-rails-tutorial
/app/views/shared/_error_messages.html.erb
<% if object.errors.any?%><div id="error_explanation"><div class="alert alert-error">表单包含 <%=pluralize(object.errors.count, "error") %>.
<ul><% object.errors.full_messages.each do |msg|%><li>* <%= msg%></li><%结束%>
<%结束%>
在视图中
<%= form_for(@user) do |f|%><%= render 'shared/error_messages', object: f.object %><%= f.label :name %><%= f.text_field :name %><%= f.label :email %><%= f.text_field :email %><%= f.label :password %><%= f.password_field :password %><%= f.label :password_confirmation, "Confirmation" %><%= f.password_field :password_confirmation %><%= f.submit 创建我的账户",类别:btn btn-large btn-primary"%><%结束%>
I have the following two _forms:
user form
<%= simple_form_for(@user, :url => @target) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :email, :label => "User Email" %>
<%= f.input :password, :label => "User Password" %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.button :submit %>
<% end %>
tenant form
<%= simple_form_for(@tenant, :url => @target) do |f| %>
<% if @tenant.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@tenant.errors.count, "error") %> prohibited this tenant from being saved:</h2>
<ul>
<% @tenant.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :name, :label => 'Name', :required => true %>
<%= f.input :billing_email, :label => 'Email', :required => true %>
<%= f.input :country, :label => 'Country', :required => true %>
<%= f.button :submit %>
<% end %>
I have come across the following post from stackoverflow f.error_messages in Rails 3.0
Here there is method so that error messages can be returned from the simple form by using f.error_messages but I have been unable to get this working as I am unsure whereabout this method should be saved. Anyone got any hints? The method is as follows:
class StandardBuilder < ActionView::Helpers::FormBuilder
def error_messages
return unless object.respond_to?(:errors) && object.errors.any?
errors_list = ""
errors_list << @template.content_tag(:span, "There are errors!", :class => "title-error")
errors_list << object.errors.full_messages.map { |message| @template.content_tag(:li, message) }.join("\n")
@template.content_tag(:ul, errors_list.html_safe, :class => "error-recap round-border")
end
end
解决方案
Just add error: false to your inputs this will not clear the css but will clear the inline errors
f.input error: false or :error => false
Edit:
From http://ruby.railstutorial.org/book/ruby-on-rails-tutorial
/app/views/shared/_error_messages.html.erb
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
in VIEW
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
这篇关于在顶部 <div> 中显示 simple_form 错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!