进出口创造了这样的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