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

Answers


You can use the presence validator:

validates :name, :presence => true

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

Need Your Help

twitter feed on website that is in a repeating box confusion

twitter website repeat

I have a twitter feed on my website and i'm trying to put it in a box that has a top, a middle section that repeats as needed to accommodate text and a bottom image as well as an image below the bo...

How can I prevent a form from being submitted more than once within 5 minutes?

php javascript-injection address-bar

I recently found a huge security problem with my PM system that allows users to send a message as much as they want with a for loop in the address bar. Someone put this into the address bar:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.