簡體   English   中英

使用活動記錄驗證,Rails驗證在模型中不起作用

[英]rails validation is not working in model using active-record validation

我已經對其進行驗證的模型是:

class DeliveryTimePerformer < ActiveRecord::Base
  belongs_to :delivery_time
  belongs_to :performer
    validates :amount, numericality: { greater_than: 0 }

end
class ClipCategoryPerformer < ActiveRecord::Base
  belongs_to :clip_category
  belongs_to :performer
  validates :amount, numericality: { greater_than: 0 }
end
class DeliveryTimePerformer < ActiveRecord::Base
  belongs_to :delivery_time
  belongs_to :performer
    validates :amount, numericality: { greater_than: 0 }

end

這是html部分。

   <label>Categories</label>
          <% ClipCategory.all.each do |category| %>
              <%= form_tag "update_amount", :remote => :true, :builder => Judge::FormBuilder do %>
              <div class="span12">
                <div class="category_check pull-left" data-category="category_text_<%= category.id %>">
                  <%= check_box_tag "category", 1, ( @performer.clip_category_ids.include?(category.id) ? "checked" : false ), :class => "pull-left" %>  
                  <%= label_tag "category", category.name, :class => "pull-left category_label" %> 
                </div>

                <% amount_val = ClipCategoryPerformer.where("clip_category_id = ? AND performer_id = ?", category.id, current_user.performer.id ).first %>
                <div id="category_text_<%= category.id%>" class="category_text span10 pull-right">
                 <% if amount_val %>
                  <%= number_field_tag "amount", @amount, :class => "pull-left input-medium", :value => amount_val.amount,:validate => true %>
                  <% else %>
                  <%= number_field_tag "amount", @amount, :class => "pull-left input-medium", :value => nil,:validate => true %>
                  <% end %>
                  <%= hidden_field_tag "category_id", category.id %>
                  <%= submit_tag "Update" %>
                 </div>
              </div>
                <br />
              <% end %>

          <% end %>

        </div>

這僅適用於ClipCategoryPerformer。 其余模型在同一頁面中具有相似的代碼。 它們是使用以下代碼通過ajax提交的:

 def update_amount
    #@ccp = ClipCategoryPerformer.create(:clip_category_id => params[:category], :performer_id => current_user.performer.id, :amount => params[:amount].to_f)

    if params[:category] == "1"
      puts "checked"
      @ccp = ClipCategoryPerformer.create(:clip_category_id => params[:category_id], :performer_id => current_user.performer.id, :amount => params[:amount].to_f)
    else
      puts "unchecked"
      #      d = ClipCategoryPerformer.find_by(:clip_category_id == params[:category] && :performer_id == current_user.performer.id)
      d = ClipCategoryPerformer.where("clip_category_id = ? AND performer_id = ?", params[:category_id], current_user.performer.id )

      d.destroy_all
    end
    #puts checkbox(category).checked
    #    rails.logger
    respond_to do |format|
      format.js { @ccp }
    end
  end

驗證不會發生。 當我單擊更新按鈕時。 它不會驗證,但會增加值。 我究竟做錯了什么? 我也嘗試使用法官寶石。 但是我不確定這是否會奏效,因為它本身無法奏效。 有任何想法嗎?

嘗試一下:

validates_numericality_of :amount, greater_than: 0, on: :update_amount

或者,您可以嘗試取消on: :update_amount

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM