2011-02-15 59 views
1

這是我收到的錯誤:如何在Rails 3中添加驗證到部分?

ArgumentError in Home#index 

Showing /app/views/clients/_form.html.erb where line #6 raised: 

You need to supply at least one validation 
Extracted source (around line #6): 

3: render :partial => "clients/form", 
4:   :locals => {:client => client} 
5: -%> 
6: <% client ||= Client.new 
7: new_client = client.new_record? %> 
8: <%= form_for(client, :html => { :class=>"ajax-form", :id => "client-ajax-form"}, :remote => true, :disable_with => (new_client ? "Adding..." : "Saving...")) do |f| %> 
9: <div class="validation-error" style="display:none"></div> 

我的客戶端模式是這樣的:

class Client < ActiveRecord::Base 
    # the user model for the client 
    belongs_to :user 

    has_many :projects, :order => 'created_at DESC', :dependent => :destroy 

    #The following produces the designers for a particular client. 
    #Get them from the relations where the current user is a client. 
    has_one :ownership, :dependent => :destroy 

    has_one :designer, :through => :ownership 


    validates :name, :presence => true, 
        :length => {:minimum => 1, :maximum => 128} 

    validates :number_of_clients 

    def number_of_clients 
    Authorization.current_user.clients.count <= Authorization.current_user.plan.num_of_clients  
    end 

end 

這是怎麼app/views/client/_form.html.erb部分外觀:

<%# 
    Edit a single client 
    render :partial => "clients/form", 
     :locals => {:client => client} 
-%> 
<% client ||= Client.new 
    new_client = client.new_record? %> 
<%= form_for(client, :html => { :class=>"ajax-form", :id => "client-ajax-form"}, :remote => true, :disable_with => (new_client ? "Adding..." : "Saving...")) do |f| %> 
    <div class="validation-error" style="display:none"></div> 
     <div> 
     <label for="client_name"><span class="icon name-icon"> </span></label> 
     <input type="text" class="name" size="20" name="client[name]" id="client_name" value="<%= client.name %>" > <%= f.submit(new_client ? "Add" : "Save", :class=> "green awesome")%> 
    </div> 
<% end %> 
<% content_for(:deferred_js) do %> 
// From the Client Form 
$('#client-ajax-form') 
    .bind("ajax:success", function(evt, data, status, xhr){ 
    console.log("Calling Step View"); 
    compv.updateStepView('client', xhr); 
}); 
<% end %> 

如何解決那個錯誤?

回答

5

問題是由模型中的以下行造成的:

validates :number_of_clients 

當您使用只會驗證(S到底),你必須遵循預設的軌道驗證像你的名字驗證一樣。但是,當您使用自定義方法進行驗證時,應該使用驗證。所以這應該工作:

validate :number_of_clients