我自己写了一个表单的修改验证,为什么验证信息不能提示呢?
我想要的效果
Form的代码
<%=form_tag({:action=>"region_save" , :id=>@region.id}) %>
<table class="datatable" width='90%'>
<tr bgcolor='#BFBFD2'><td colspan='2' style="valign:middle;"><h2>Region</h2></td></tr>
<tr><td>Region Name:</td><td><%= text_field("region","name") %></td></tr>
<tr bgcolor='#FFFCDF'><td>Region IP:</td><td><%= text_field("region","ip")%></td></tr>
<tr><td colspan='2'><%=submit_tag %></td></tr>
</table>
</form>
Controller的代码
def region_save
region_id=params[:id]
region_name=params[:region][:name]
region_ip=params[:region][:ip]
if (region_id.nil?)
region=Region.new()
else
region=Region.find(region_id)
end
region[:name]=region_name
region[:ip]=region_ip
region.save()
redirect_to :action => "region_list"
end
Model的代码
class Region < ActiveRecord::Base
validates_uniqueness_of :name
validates_uniqueness_of :ip
validates_presence_of :name,
:message=>"Please input a name to region."
validates_format_of :ip,
:with => /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,
:message=>"Please input a right format of ip address."
end