2

我想检查我的应用程序,输入值应该是整数,不能为空或 nil。

class Event < ActiveRecord::Base
  validates :event_cost, :presence => { :message => "can't be empty..." }

  validates :event_cost, 
              :numericality => { :only_integer => { :message => "should be only integer" } } 
end

因此,如果用户在该字段中不打印任何内容,我会收到消息"Event cost is not a number"。我怎样才能设置我自己的消息而不是那个?

4

3 回答 3

5

尝试

class Event < ActiveRecord::Base
  validates :event_cost, :presence => { :message => "can't be empty..." }

  validates :event_cost, 
              :numericality => { :message => "Your own message", :only_integer => true } 
end
于 2012-05-09T14:13:28.683 回答
1

试试这个,它会正常工作,因为你没有将 only_integer 设置为 true 它不能正常工作

:event_cost, 
          :numericality => { :only_integer => true, :message => "should be only integer"}
于 2012-05-09T14:11:28.277 回答
1

试试这个

验证 :event_cost, :numericality => { :only_integer => true,{ :message => "应该只是整数" } }

于 2012-05-09T14:13:43.370 回答