我已经完成验证的模型是:
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
验证不会发生。当我单击更新按钮时。它不验证但添加值。我究竟做错了什么?我也尝试使用法官宝石。但我不确定这会起作用,因为这本身不起作用。有任何想法吗?