客户端表单验证不适用于关联

时间:2012-12-11 06:24:53

标签: ruby-on-rails

  1. 我有两个模型部门和地址,他们是相关的 彼此,但当我使用客户端表单验证地址 模型字段未经过验证。 javascript for 没有生成验证关联模型。
  2. 部门模型

    class Department < ActiveRecord::Base   
        attr_accessible :agency_head, :agency_head_rank, :department_type_id, :fax, :name,
                        :phone, :address_attributes
        validates :name , :length => { :minimum => 2 } , :presence => true
        validates :department_type_id , :agency_head,:agency_head_rank,:phone,:fax, :presence => true
    
        belongs_to :department_type
        belongs_to :address, :class_name => "Address", :foreign_key => "address_id"   
        accepts_nested_attributes_for :address,:allow_destroy => true
    end
    
  3. 地址模型

    class Address < ActiveRecord::Base   
        attr_accessible :state, :street1, :street2, :town, :zipcode, :county   
        validate :street1 ,:street2,:town,:state,:county,:zipcode ,:presence => true   
        has_one :department  
    end
    
  4. 查看页

            <h1 id="form-title">Add New Department</h1>
    
            <%=form_for @department ,:validate=>true do |f|%>   
    <div    id="respond">
                <% if @department.errors.any? %>
                <div id="error_explanation">
                    <h2><%= pluralize(@department.errors.count, "error") %> prohibited this post from being saved:</h2>
    
                    <ul>
                        <% @department.errors.full_messages.each do |msg| %>
                        <li>
                            <%= msg %>
                        </li>
                        <% end %>
                    </ul>
                </div>
                <% end %>
    
                <p class="comment-form-author">
                    <label for="Department Name">Department&nbsp;Name</label><span class="required">*</span>
                    <%= f.text_field :name, :size => 30, 'aria-required' => 'true'  %>
                </p>
                <p class="comment-form-author">
                    <label for="Department Type">Department&nbsp;Type</label>
                    <%= f.collection_select :department_type_id, @dept_types, :id, :name, :prompt => true %>
                </p>
    
                <%= f.fields_for :address, @department.address ,:validate=> true do |addr| %>
                <p class="comment-form-author">
                    <label for="Street1">Street 1</label>
                    <%= addr.text_field :street1 , :size => 30, 'aria-required' => 'true'  %>
                </p>
                <p class="comment-form-author">
                    <label for="Street 2">Street 2</label>
                    <%= addr.text_field :street2, :size => 30, 'aria-required' => 'true'  %>
                </p>
                <p class="comment-form-author">
                    <label for="Town">Town</label>
                    <%= addr.text_field :town, :size => 30, 'aria-required' => 'true'  %>
                </p>
    
                <p class="comment-form-author">
                    <label for="State">State</label>
                    <%= addr.text_field :state, :size => 30, 'aria-required' => 'true'  %>
                </p>
                <p class="comment-form-author">
                    <label for="State">County</label>
                    <%= addr.text_field :county, :size => 30, 'aria-required' => 'true'  %>
                </p>
    
                <p class="comment-form-author">
                    <label for="Zip Code">Zip Code</label>
                    <%= addr.text_field :zipcode, :size => 30, 'aria-required' => 'true'  %>
                </p>
    
                <% end %>
    
                <p class="comment-form-author">
                    <label for="agency head">Agency Head</label><span class="required">*</span>
                    <%= f.text_field :agency_head, :size => 30, 'aria-required' => 'true'  %>
                </p>
                <p class="comment-form-author">
                    <label for="agency head rank">Agency Head Rank</label><span class="required">*</span>
                    <%= f.text_field :agency_head_rank, :size => 30, 'aria-required' => 'true'  %>
                </p>
    
                <p class="comment-form-author">
                    <label for="phone">Phone</label><span class="required">*</span>
                    <%= f.text_field :phone, :size => 30, 'aria-required' => 'true'  %>
                </p>
                <p class="comment-form-author">
                    <label for="fax">Fax</label><span class="required">*</span>
                    <%= f.text_field :fax, :size => 30, 'aria-required' => 'true'  %>
                </p>
                            </div>
                        <div class="spacer">
                <br/>
                <center>
                    <%= f.button " Submit ", :id => 'my_login_form_button' %>
                </center>           </div>          <% end %>       </div> </div>   </div> </div> </div>
    

2 个答案:

答案 0 :(得分:1)

您可以使用Jquery验证来验证表单。它比手动操作容易得多。

答案 1 :(得分:-1)

我发现我的错误在地址模型中使用了这个。

验证:street1,:street2,:town,:state,:county,:zipcode,:presence =&gt;真