简体   繁体   中英

rails validation is not working in model using active-record validation

The models for which I have done validations are:

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

This is the html part.

   <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>

This is only for ClipCategoryPerformer. The rest of the models have similar code in the same page. They are submitted via ajax using the following code:

 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

The validations will not happen. When I click the update button. It doesn't validate but add the value. What am I doing wrong? Also I tried using the judge gem. But I am not sure that will work as this itself is not working. Any ideas?

How about trying this:

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

Or you could try leaving off the on: :update_amount

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM