Review
Topics Covered: Validations; custom validations
Exercises
E1: Validations Review
- Where do you write validations and why? Are there any validations that go in the controller?
- Why does the code below respond false on
user.save?
. When does a user.valid?
return true?
>> user = User.new(:name => "", :email => "mhartl@example.com")
>> user.valid?
>> user.save
- What's one line of code to check if a User attribute has any errors? Hint: We did this in the console.
- Is it possible to validate relations? Think of validations for the relations in the Twitter app. Hint: Google for Rails relations validations.
E2: I Need Validation
Let's add some validations to MeowBook because, well, some of us are just insecure sometimes.
- Make it so the handle must be at least 4 characters long (including the @).
- Ensure that the posts have a body.
- Bonus: Make it so the handle must start with an @, but can't have a number as its second character (you'll need another custom validator).
Projects
The Real Deal
By the end of the day you'll have to present your project specifications to Micah! This is the time to really crank out some good thinking about how you want your project to turn out, so put in the work now to save lots of time in the future.