進出口創造了這樣的Rails:顯示控制器添加錯誤信息
product = Product.new
product.name = params['name']
if params['quantity']=="" then product.errors.add(:base, "Quantity must be filled") end
if !product.save
@product = product
render "index"
end
和IM一樣,
<% @product.errors.each do |attr, msg| %>
<li> <%= msg %></li>
<% end %>
觀看錯誤控制器動作新產品顯示所有錯誤,但只有:基誤差不顯示。我如何顯示它們?
數量列不是產品型號的一部分。
(解決方案)的工作代碼:
product.valid?
if params['quantity']=="" then product.errors.add(:base, "error") end
if product.errors.any?
@product = product
render "index"
return
else
product.save
好像你只是想'數量'字段驗證... – meagar