Rails 3

时间:2015-05-05 09:22:23

标签: ruby ruby-on-rails-3.2

我有一个关于rails验证的问题。我已经验证了模型文件中的所有字段但是当我输入一些错误的数据时没有显示错误消息。由于我的表单包含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 &amp; Time :</div></span>
              <%= f.text_field :dat ,:class => "form-control add-on",placeholder:"Date &amp; 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>

即使上面的页面正在渲染,但我也无法知道@vendor对象在DB中提交数据之前是如何创建的。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

@satya,我们有两种选择,您可以将def create_vendor下的替换为@vendor -

1)如果@vendor和@ vendor.id.present?

2)如果@vendor和@ vendor.errors.blank?和@ vendor.errors.messages.blank?

因为create方法总是根据验证返回保存或不保存的对象!

你可以使用save代替create,因为save会给你true或false而不会返回对象!