2
  1. 我有两个模型部门和地址,它们相互关联,但是当我使用客户端表单验证时,地址模型字段没有得到验证。未生成用于验证关联模型的 javascript。
  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>
    
4

2 回答 2

1

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

于 2012-12-12T07:06:50.623 回答
-1

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

验证 :street1 ,:street2,:town,:state,:county,:zipcode ,:presence => true

于 2012-12-14T13:27:37.010 回答