Rails 3 中未显示验证错误消息
Validation error messages are not displaying in Rails 3
我有一个关于 rails 的问题 validation.I 已经验证了模型文件中的所有字段但是当我输入一些错误的数据时没有错误消息是 displaying.As 我的表单包含 Rails 的 ajax 调用我也在下面的代码 html page.check 中获取所有字段值。
vendor_add.html.erb:
<%= form_for :vendor,:url => {:action => "create_vendor" },remote: true do |f| %>
<% if @vendor.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@vendor.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @vendor.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="block-content">
<div class="timeline-container">
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Registration No :</div></span>
<%= f.text_field :reg_no ,:class => "form-control",:id => "reg" %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left" ><span class="input-group-addon text-left"><div class="leftsidetextwidth">Date & Time :</div></span>
<%= f.text_field :dat ,:class => "form-control add-on",placeholder:"Date & Time",:required => true,:id => 'datetimepicker4',:onclick => "get_date();" %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Name :</div></span>
<%= f.text_field :v_name ,:class => "form-control",placeholder:"Name",:required => true %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Father Name :</div></span>
<%= f.text_field :f_name ,:class => "form-control",placeholder:"Father Name",:required => true %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left" ><span class="input-group-addon text-left"><div class="leftsidetextwidth">Date of Birth :</div></span>
<%= f.text_field :dob ,:class => "form-control add-on",placeholder:"Date of Birth",:required => true,:id => "dp2",:onclick => "get_dob();" %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Mob. No :</div></span>
<%= f.text_field :mob_no ,:class => "form-control",placeholder:"Mob. No",:required => true %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Address :</div></span>
<%= f.text_field :v_address ,:class => "form-control",placeholder:"Address",:required => true %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Service Category :</div></span>
<%= f.select(:s_catagory,options_for_select([['Wood','Wood'],['Puja Samagree','Puja Samagree'],['Sweeper','Sweeper'],['Photo Grapher','Photo Grapher'],['Burning Assistant','Burning Assistant'],['BRAHMIN','BRAHMIN']]),{:prompt => "Select type" },{:class => 'form-control'}) %>
</div>
<div class="clearfix"></div>
<div class="tbpaddingdiv1 text-center">
<%= f.submit "Submit",:class => "btn btn-success" %>
</div>
</div>
</div>
<% end %>
Vendor.rb:
class Vendor < ActiveRecord::Base
attr_accessible :dat, :dob, :f_name, :mob_no, :reg_no, :s_catagory, :v_address, :v_name
validates :v_name, :presence => true,:length => {:minimum => 3}
validates :f_name, :presence => true,:length => {:minimum => 3}
validates :mob_no, :presence => true, :length => {:in => 1..10},format: { with: /\d{3}-\d{3}-\d{4}/, message: "bad format" }
validates :dob, :date => {:before => Time.now }
validates :s_catagory,:presence => true
end
vendors_controller.rb:
class VendorsController < ApplicationController
def vendor_add
@vendor=Vendor.new
if params[:v_id]
@vendor=Vendor.find(params[:v_id])
end
respond_to do |format|
format.html
format.js
end
end
def create_vendor
if params[:vendor][:s_catagory]=="Wood"
catagory="Woods"
elsif params[:vendor][:s_catagory]=="Puja Samagree"
catagory="GOODS"
elsif params[:vendor][:s_catagory]=="Sweeper"
catagory="SWD"
elsif params[:vendor][:s_catagory]=="Photo Grapher"
catagory="PHOTO"
elsif params[:vendor][:s_catagory]=="Burning Assistant"
catagory="BURNING"
else
catagory="BRAHMIN"
end
@vendor= Vendor.create(:v_name => params[:vendor][:v_name],:v_address => params[:vendor][:v_address],:reg_no => params[:vendor][:reg_no],:f_name => params[:vendor][:f_name],:mob_no => params[:vendor][:mob_no],:dob => params[:vendor][:dob],:s_catagory => catagory,:dat => params[:vendor][:dat] )
if @vendor
flash[:notice]="Vendor data has submitted"
flash[:color]="valid"
else
flash[:alert]="Data could not submit"
flash[:color]="invalid"
render 'vendor_add'
end
end
def vendor_edit
@vendor=Vendor.find(params[:v_id])
end
def update_vendor
@vendor=Vendor.find(params[:v_id])
if @vendor.update_attributes(params[:vendor])
flash[:notice]="Vendor data has updated"
flash[:color]="valid"
else
flash[:alert]="Data could not submit"
flash[:color]="invalid"
render 'vendor_add',:v_id => params[:v_id]
end
end
end
create_vendor.js.erb:
$("#vendor-total-div").css("display", "block");
$("#vendor-div").html("<%= escape_javascript (render 'vendor_output_table' ) %>");
$("#vendor-div").slideDown(350);
_vendor_output_table.html.erb:
<table class="table table-bordered">
<thead>
<tr>
<th class="text-center">Sl. No</th>
<th class="text-center">Date</th>
<th class="text-center">Registration No</th>
<th class="text-center">Name</th>
<th class="text-center">Mob. No</th>
<th class="text-center">Service Category</th>
<th class="text-center">Status</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-center"><%= @vendor.id %></td>
<td class="text-center"><%= @vendor.dat %></td>
<td class="text-center"><%= @vendor.reg_no %></td>
<td class="text-center"><%= @vendor.v_name %></td>
<td class="text-center"><%= @vendor.mob_no %></td>
<td class="text-center"><%= @vendor.s_catagory %></td>
<td class="text-center"><%= @vendor.id %></td>
<td class="text-center">
<div class="btn-group">
<a href="/vendor_add?v_id=<%= @vendor.id %>" class="btn btn-xs btn-success" data-toggle="tooltip" data-placement="top" title="Edit">
<i class="fa fa-edit"></i>
</a>
</div>
</td>
</tr>
</tbody>
</table>
我也不知道上面的页面是否正在呈现,但是在 DB.Please 中提交数据之前如何创建 @vendor 对象帮助我解决这个问题。
@satya,我们有两个选项,您可以将 def create_vendor 下的 if @vendor 替换为 -
1)if @vendor and @vendor.id.present?
2) 如果@vendor 和@vendor.errors.blank?和@vendor.errors.messages.blank?
因为创建方法总是return对象是否保存基于验证!
或
您可以使用保存而不是创建,因为保存会给您 true 或 false 而不是 return 对象!
我有一个关于 rails 的问题 validation.I 已经验证了模型文件中的所有字段但是当我输入一些错误的数据时没有错误消息是 displaying.As 我的表单包含 Rails 的 ajax 调用我也在下面的代码 html page.check 中获取所有字段值。
vendor_add.html.erb:
<%= form_for :vendor,:url => {:action => "create_vendor" },remote: true do |f| %>
<% if @vendor.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@vendor.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @vendor.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="block-content">
<div class="timeline-container">
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Registration No :</div></span>
<%= f.text_field :reg_no ,:class => "form-control",:id => "reg" %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left" ><span class="input-group-addon text-left"><div class="leftsidetextwidth">Date & Time :</div></span>
<%= f.text_field :dat ,:class => "form-control add-on",placeholder:"Date & Time",:required => true,:id => 'datetimepicker4',:onclick => "get_date();" %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Name :</div></span>
<%= f.text_field :v_name ,:class => "form-control",placeholder:"Name",:required => true %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Father Name :</div></span>
<%= f.text_field :f_name ,:class => "form-control",placeholder:"Father Name",:required => true %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left" ><span class="input-group-addon text-left"><div class="leftsidetextwidth">Date of Birth :</div></span>
<%= f.text_field :dob ,:class => "form-control add-on",placeholder:"Date of Birth",:required => true,:id => "dp2",:onclick => "get_dob();" %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Mob. No :</div></span>
<%= f.text_field :mob_no ,:class => "form-control",placeholder:"Mob. No",:required => true %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Address :</div></span>
<%= f.text_field :v_address ,:class => "form-control",placeholder:"Address",:required => true %>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Service Category :</div></span>
<%= f.select(:s_catagory,options_for_select([['Wood','Wood'],['Puja Samagree','Puja Samagree'],['Sweeper','Sweeper'],['Photo Grapher','Photo Grapher'],['Burning Assistant','Burning Assistant'],['BRAHMIN','BRAHMIN']]),{:prompt => "Select type" },{:class => 'form-control'}) %>
</div>
<div class="clearfix"></div>
<div class="tbpaddingdiv1 text-center">
<%= f.submit "Submit",:class => "btn btn-success" %>
</div>
</div>
</div>
<% end %>
Vendor.rb:
class Vendor < ActiveRecord::Base
attr_accessible :dat, :dob, :f_name, :mob_no, :reg_no, :s_catagory, :v_address, :v_name
validates :v_name, :presence => true,:length => {:minimum => 3}
validates :f_name, :presence => true,:length => {:minimum => 3}
validates :mob_no, :presence => true, :length => {:in => 1..10},format: { with: /\d{3}-\d{3}-\d{4}/, message: "bad format" }
validates :dob, :date => {:before => Time.now }
validates :s_catagory,:presence => true
end
vendors_controller.rb:
class VendorsController < ApplicationController
def vendor_add
@vendor=Vendor.new
if params[:v_id]
@vendor=Vendor.find(params[:v_id])
end
respond_to do |format|
format.html
format.js
end
end
def create_vendor
if params[:vendor][:s_catagory]=="Wood"
catagory="Woods"
elsif params[:vendor][:s_catagory]=="Puja Samagree"
catagory="GOODS"
elsif params[:vendor][:s_catagory]=="Sweeper"
catagory="SWD"
elsif params[:vendor][:s_catagory]=="Photo Grapher"
catagory="PHOTO"
elsif params[:vendor][:s_catagory]=="Burning Assistant"
catagory="BURNING"
else
catagory="BRAHMIN"
end
@vendor= Vendor.create(:v_name => params[:vendor][:v_name],:v_address => params[:vendor][:v_address],:reg_no => params[:vendor][:reg_no],:f_name => params[:vendor][:f_name],:mob_no => params[:vendor][:mob_no],:dob => params[:vendor][:dob],:s_catagory => catagory,:dat => params[:vendor][:dat] )
if @vendor
flash[:notice]="Vendor data has submitted"
flash[:color]="valid"
else
flash[:alert]="Data could not submit"
flash[:color]="invalid"
render 'vendor_add'
end
end
def vendor_edit
@vendor=Vendor.find(params[:v_id])
end
def update_vendor
@vendor=Vendor.find(params[:v_id])
if @vendor.update_attributes(params[:vendor])
flash[:notice]="Vendor data has updated"
flash[:color]="valid"
else
flash[:alert]="Data could not submit"
flash[:color]="invalid"
render 'vendor_add',:v_id => params[:v_id]
end
end
end
create_vendor.js.erb:
$("#vendor-total-div").css("display", "block");
$("#vendor-div").html("<%= escape_javascript (render 'vendor_output_table' ) %>");
$("#vendor-div").slideDown(350);
_vendor_output_table.html.erb:
<table class="table table-bordered">
<thead>
<tr>
<th class="text-center">Sl. No</th>
<th class="text-center">Date</th>
<th class="text-center">Registration No</th>
<th class="text-center">Name</th>
<th class="text-center">Mob. No</th>
<th class="text-center">Service Category</th>
<th class="text-center">Status</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-center"><%= @vendor.id %></td>
<td class="text-center"><%= @vendor.dat %></td>
<td class="text-center"><%= @vendor.reg_no %></td>
<td class="text-center"><%= @vendor.v_name %></td>
<td class="text-center"><%= @vendor.mob_no %></td>
<td class="text-center"><%= @vendor.s_catagory %></td>
<td class="text-center"><%= @vendor.id %></td>
<td class="text-center">
<div class="btn-group">
<a href="/vendor_add?v_id=<%= @vendor.id %>" class="btn btn-xs btn-success" data-toggle="tooltip" data-placement="top" title="Edit">
<i class="fa fa-edit"></i>
</a>
</div>
</td>
</tr>
</tbody>
</table>
我也不知道上面的页面是否正在呈现,但是在 DB.Please 中提交数据之前如何创建 @vendor 对象帮助我解决这个问题。
@satya,我们有两个选项,您可以将 def create_vendor 下的 if @vendor 替换为 -
1)if @vendor and @vendor.id.present?
2) 如果@vendor 和@vendor.errors.blank?和@vendor.errors.messages.blank?
因为创建方法总是return对象是否保存基于验证!
或
您可以使用保存而不是创建,因为保存会给您 true 或 false 而不是 return 对象!