在我的验证中没有显示错误消息 不正确的保存数据显示错误消息,而不是显示错误消息,
Missing template employees/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}
我的模特是,
validates :emp_firstname, :presence=> {:message=>" cannot be blank"},
:format => { :with => /\A[a-zA-Z@\.\s]*\z/, :message => "only alphabets."}
validates :emp_lastname, :presence=> {:message=>" cannot be blank"},
:format => { :with => /\A[a-zA-Z@\.\s]*\z/, :message => "only alphabets."}
validates :address
validates :email_id, :presence=> {:message => "cannot be blank."},
:format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :message => "Give Correct format"}
我的观看页面是,
<% if @employee.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@employee.errors.count, "error") %> Errors
</h2>
<ul>
<% @employee.errors.full_messages.each do |message| %>
<li>
<%= message %>
</li>
<% end %>
</ul>
</div>
<% end %>
<table align="center">
<tr>
<td>Firstname<span class="required"></span></td>
<td><%= f.text_field :emp_firstname, :required => true%></td>
</tr>
<tr>
<td>Lastname<span class="required"></span></td>
<td><%= f.text_field :emp_lastname, :required => true %></td>
</tr>
<tr>
<td>Address<span class="required"></span></td>
<td><%= f.text_area :address, :required => true %></td>
</tr>
<tr>
<td>Email_id<span class="required"></span></td>
<td><%= f.text_field :email_id, :required => true%></td>
</tr>
controller create method,
def create
@employee = Employee.new
@employee.emp_firstname = params[:employee][:emp_firstname]
@employee.emp_lastname = params[:employee][:emp_lastname]
@employee.address = params[:employee][:address]
@employee.email_id = params[:employee][:email_id]
@employee.comp_id = params[:company][:comp_id]
@employee.dom_id = params[:domain][:dom_id]
if @employee.save
respond_to do |format|
format.html {flash[:notice] = 'Employee was successfully created.' and redirect_to action: "index"}
end
end
end
保存新员工时,不会显示验证错误消息。显示模板缺失错误。当我在现场提供正确的格式时,会保存员工的详细信息。请给我正确的方法来显示验证错误消息 谢谢。
更新用
在验证之前,邮件是错误的,因此它会回滚。
Parameters: {"utf8"=>"✓", "authenticity_token"=>"2w/EyPI1+MGacWDXfX7fIE6VC5uDMlD9vbho+gXra1LD7U3XOzhLHoV5XSLmWwG5QY8tw9bOgMggMrt1/netdQ==", "employee"=>{"emp_firstname"=>"Aishwarya", "emp_lastname"=>"Prabha", "address"=>"xxxxxxx", "email_id"=>"dsfdsf"}, "company"=>{"comp_id"=>"1"}, "domain"=>{"dom_id"=>"3"}, "commit"=>"Save"}(0.2ms) BEGIN(0.2ms) ROLLBACK
更正邮件后,也会回滚。没有保存。
Parameters: {"utf8"=>"✓", "authenticity_token"=>"zxMM4m0F+aSejV+8UMk3gv3M3H2ueBz7xCppMEEQNqvX8YX9pAhKe4GFYknL7Okb8tb6JfuEzM5ZoLq/uozwjA==", "employee"=>{"emp_firstname"=>"Aishwarya", "emp_lastname"=>"Prabha", "address"=>"xxxxxxx", "email_id"=>"example@mail.com"}, "company"=>{"comp_id"=>""}, "domain"=>{"dom_id"=>""}, "commit"=>"Save"}(0.1ms)BEGIN(0.1ms)ROLLBACK
我的表格中有下拉列表不起作用。所以com_id和dom_id变空了。
答案 0 :(得分:3)
如果成功保存对象而没有任何验证错误,您将重定向到索引操作,但如果出现错误怎么办?
您还应该处理错误情况,以便如果对象未保存,则应该呈现新表单,其中显示错误消息。
根据您的错误代码,它正在搜索不存在的创建视图文件,因此您应该明确声明
渲染'新' //在其他情况下检查此行
class EmployeesController < ApplicationController
def create
@employee = Employee.new
@employee.emp_firstname = params[:employee][:emp_firstname]
@employee.emp_lastname = params[:employee][:emp_lastname]
@employee.address = params[:employee][:address]
@employee.email_id = params[:employee][:email_id]
@employee.comp_id = params[:company][:comp_id]
@employee.dom_id = params[:domain][:dom_id]
if @employee.save
respond_to do |format|
format.html {flash[:notice] = 'Employee was successfully created.' and redirect_to action: "index"}
end
else
render 'new' // This will show your error messages
end
end
结束
因此,如果对象的验证失败,它将呈现为新操作,然后将显示验证消息。