Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a field required in Rails?

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

inquiry.rb:

class Inquiry < ActiveRecord::Base   attr_accessible :address, :email_id, :gender, :message, :mobile_number, :name end 
like image 627
Loganayaki Palanisamy Avatar asked Aug 23 '13 08:08

Loganayaki Palanisamy


People also ask

What is Activerecord in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

How does validation work in Rails?

Rails validation defines valid states for each of your Active Record model classes. They are used to ensure that only valid details are entered into your database. Rails make it easy to add validations to your model classes and allows you to create your own validation methods as well.

What is the difference between validate and validates in rails?

validates is used for normal validations presence , length , and the like. validate is used for custom validation methods validate_name_starts_with_a , or whatever crazy method you come up with. These methods are clearly useful and help keep data clean. That test fails.


1 Answers

You can use the presence validator:

validates :name, :presence => true 
like image 187
Mischa Avatar answered Oct 05 '22 01:10

Mischa