0
我試着設置一個表單驗證,以確保至少1個和至多3個標籤必須包含在窗體中。但它不起作用,因爲仍然處理空的表單,但是具有4個逗號分隔標籤的表單將得到正確驗證。模型未驗證
控制器
def update
@product = Product.find(params[:id])
current_user.tag(@product, :with => params[:product][:tag_list], :on => :tags)
if @product.update_attributes(params[:product])
redirect_to :root, :notice => "Added"
else
render :action => 'edit'
end
end
表
<%= form_for @product do |f| %>
<%= f.label :tag_list, "Your tags" %> <%= f.text_field :tag_list, :value => @product.tags_from(current_user) %>
<p><%= f.submit "Change" %></p>
<%= f.error_messages %>
<% end %>
模型
validate :required_info
validates_size_of :tag_list,
:maximum => 3
private
def required_info
if(tag_list.empty? and description.empty?)
errors.add_to_base "Add one"
end
end
啊哈。它被設置爲合乎邏輯的,因爲描述被故意留空,但現在它不是,邏輯或者需要驗證工作。感謝brainspark。 – Simpleton 2012-01-07 14:42:48