How to validate text isn't blank in Rails

前端 未结 3 592
北荒
北荒 2020-12-15 03:32

if I do

  validates :body, :presence => true, :length => {:maximum => 30000, :message => \' is a bit long...\'}
  validates :body, :length =>         


        
3条回答
  •  Happy的楠姐
    2020-12-15 04:32

    Rails adds the handy method blank? which checks for false, nil and empty strings as described here.
    Rails also adds the handy validator allow_blank: false.

    So in your case it should be:

    validates :body, presence: true, allow_blank: false


    Edit (original answer above):

    As stated in the answer below, allow_blank: false is not needed as that's the default behaviour of presence: true.

提交回复
热议问题