In this conclusion to a four-part article series on building a basic Ruby-on-Rails application, you'll learn how to add validations, build the basic scaffolding, and more. This article is excerpted from chapter three of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).
Finishing a Basic Ruby-on-Rails Application - Adding Validations (Page 2 of 3 )
You might be wondering what would happen if you tried to save a new event without giving it any information. Go ahead and try doing that. You’ll see that Rails doesn’t care. In fact, it’s the Eventmodel that doesn’t care. This is because in Rails, the rules for data integrity (such as required fields) are the responsibility of the model.
To add basic validation for required fields, open theEventmodel inapp/models/event.rband add the validator methods shown in Listing 3-5 inside the class body.
Listing 3-5. Validation Added to the app/models/event.rb File
class Event < ActiveRecord::Base validates_presence_of :title, :location end
Save the file and try creating an empty event again. Instead of saving the record, Rails will display a formatted error message, as shown in Figure 3-5.
Figure 3-5. Error messages for an event
If you’ve done any web development before, you know that validating fields is a major nuisance. Thankfully, Rails makes it easy.
Note Notice that you don’t need to restart the web server when you make changes to your project files. This is a convenience provided by Rails when running in development mode.
We’ll go through all the specifics of model validations in Chapter 5. For now, we’re using only the most primitive methods of protecting our data. It shouldn’t surprise you that Active Record is capable of much more involved validations, such as making sure that a numeric value was entered, validating that data is in the correct format using regular expressions, and ensuring unique values, among other checks.
Note Regular expressions (regex for short) are expressions that describe patterns in strings. They are usually used to give a concise description of a set, without having to list all elements. Like most programming languages, Ruby has built-in support for regular expressions.
At this stage, you might be wondering how all this works. We added a single line to theeventscontroller, and as if by magic, Rails took care of the rest. When you made your first controller in the previous chapter, you created actions and templates. Where are the actions and templates here? The answer is that they’re all generated dynamically, at runtime. It’s as if you created each individual action and template manually. We already talked about the advantages of this type of scaffolding (that it responds automatically to changes to the underlying model), but this won’t do you much good if you need to modify the actions and templates to customize them for your application.
Now that we have pinned down our model and are confident that it’s reasonably complete, we can pull back the curtain and have Rails generate the scaffolding, creating physical actions and templates that we can explore and tweak to our heart’s content.