我有一个带有文本字段的表格,该字段必须是数字且等于或大于5。
问题:未经验证,似乎无法显示消息
我已经尝试过:
validates :price, :numericality => {:only_integer => true, greater_than_or_equal_to: 5, message: "should happen once per year" }
这(我知道这不是一个最小值,只是测试我在另一篇文章中看到的消息功能...如果我使用非整数字符,仍然没有消息。
validates :price, format: { with: /\A\d+\z/, message: "Integer only. No sign allowed." }
主要布局具有:
<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div class="alert alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %> alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<% end %>
<% end %>
如何获取它,以便在字段输入无效时出现一条消息?
已解决:有点...
只需添加:
<% if @listing_video.errors.any? %>
<ul>
<% @listing_video.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
</ul>
<% end %>
以我的形式。
它不会像横幅atm那样弹出,但是很快就会通过bootstrap和/或CSS来解决
我通常将其从表单中删除,以避免出现“双重”错误消息,但这是需要的情况。我应该在问之前意识到这一点-
答案 0 :(得分:0)
这可能会有所帮助。简单的金额检查
validate :amount_valid?
def amount_valid?
errors.add(:amount, "cant be zero") if self.amount.zero?
errors.add(:amount, "cant be negative") if self.amount.negative?
end
如果存在错误,请在验证后在UI上显示错误:)
答案 1 :(得分:0)
已解决:
只需添加:
<% if @listing_video.errors.any? %>
<ul>
<% @listing_video.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
</ul>
<% end %>
以我的形式。
这会在输入文本字段上方为验证创建错误-这就是我想要的。