How to make a field required in Rails?

前端 未结 2 1354
眼角桃花
眼角桃花 2020-12-10 01:38

What is the simplest way to make a field required in Rails?

inquiry.rb:

class Inquiry < ActiveRecord::Base
  attr_accessible :address, :email_id,          


        
相关标签:
2条回答
  • 2020-12-10 02:03

    attr_accessible specifies a white list of model attributes that can be set via mass-assignment. This is meant to protect sensitive attributes from being overwritten by malicious users tampering with URLs or forms. It has nothing to do with validations.

    So, if you want to make the attribute presence mandatory, you have to use a validation in your model, like this one:

    validates :name, :presence => true
    
    0 讨论(0)
  • 2020-12-10 02:06

    You can use the presence validator:

    validates :name, :presence => true
    
    0 讨论(0)
提交回复
热议问题