Ruby on rails 3 在顶部显示简单表单错误消息<;部门>;
我有以下两种表格: 用户表单Ruby on rails 3 在顶部显示简单表单错误消息<;部门>;,ruby-on-rails-3,simple-form,formbuilder,Ruby On Rails 3,Simple Form,Formbuilder,我有以下两种表格: 用户表单 <%= 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 bein
<%= 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 %>
<%= 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 %>
@target)do | f |%>
禁止保存此用户:
“用户电子邮件”%>
“用户密码”%>
租户表格
<%= 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 %>
<%= 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 %>
@target)do | f |%>
禁止保存此租户:
'Name',:必选=>true%>
'电子邮件',:必填=>true%>
'国家',:必填=>true%>
我从stackoverflow收到以下邮件
这里有一种方法,可以使用f.error_消息从简单表单返回错误消息,但我无法使其工作,因为我不确定该方法应保存在何处。有人得到任何提示吗?方法如下:
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
class StandardBuilder
只需在输入中添加错误:false这不会清除css,但会清除内联错误
f.input error: false or :error => false
编辑:
从
/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 %>
表单包含。
- *
鉴于
<%= 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 %>
第一个块上缺少一个:`表单包含- *
*
有点奇怪,因为
已经产生了一个项目符号。可能会考虑删除它,即使用<代码> *
而不是项目符号,因为我觉得它更能体现项目的重要性。提示:如果你把部分放在app/views/application
而不是app/views/shared
中,那么你可以做
而不是
。