我正在尝试验证不同的输入值。例如“域”和“电子邮件”。两者都必须具有值并且必须是唯一的。所以我尝试用
validates :domain,
:presence => true,
:uniqueness => { :case_sensitive => true }
validates :email,
:presence => true,
:uniqueness => { :case_sensitive => true }
但是当我显示 flash 消息时,我得到了四个错误:
["Domain can't be blank", "Domain has already been taken", "Email can't be blank", "Email has already been taken"]
是否可以逐步检查它们?如果输入字段没有值,用户会得到
["Domain can't be blank"]
但是如果输入字段有一个值并且不是唯一的,用户会得到
["Domain has already been taken"]
我该如何实施?
编辑
下面是打印错误消息的代码:
<% [:error].each do |key| %>
<% if flash[key] %>
<div class="<%= key %>" id="flash">
<%= flash[key] %>
</div>
<% end %>
<% end %>
这里是产生错误的控制器:
def create
respond_to do |format|
# save form data
@login = Login.new(params[:login])
# if validation fails, throw error messages
if !@login.save
flash[:error] = @login.errors.to_a
end
# redirect to landingpage
format.html { redirect_to :root }
end
end