Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introduction to Ruby on Rails 7!
You have completed Introduction to Ruby on Rails 7!
Preview
Protect your database with Rails validations. Define rules to ensure data integrity, like requiring certain fields or enforcing unique records.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, hello everyone, in this video,
we're going to learn about validations,
0:00
what are validations?
0:03
Well, let me click create your own link
tree, and let me delete this tree, and
0:04
then let me create my own link tree again.
0:09
Now, validations are, for example,
let's say I click next, as you can see,
0:11
it says name must be more
than three characters long.
0:16
This is because of the extra JavaScript
that I added, but in this video,
0:19
you're going to learn how to do
validations with Ruby on Rails.
0:23
Specifically, you're going
to go in the model, and
0:26
you're going to learn
how to do validations.
0:29
Essentially, validations stop the tree
model from going into the database
0:31
without meeting certain requirements.
0:35
So, for example, I could have the name
to be over 10 characters long,
0:38
or I could have the URL
have to contain http://, or
0:44
I could have the X actually
contain the X link.
0:49
So that's what validations are, and
I'm gonna teach you them in this video,
0:53
but, one important thing that you should
know is that chatGPT is really useful
0:58
in this case.
1:03
So what I'm gonna do is ask chatGPT, and
1:04
give you an example of how we can
use AI to help us in this case.
1:06
So what I'm gonna say is,
1:10
give me a way to validate URLs in
1:14
the model, in Ruby on Rails,
1:19
I have an attribute called Instagram and
1:24
I need you to validate that it is a URL,
let's see.
1:30
So as you can see, if we go into
the app models, your model tree rb.
1:44
This code uses the "validates" method
to add a validation rule to the "your
1:51
model" class.
1:54
It checks that the "Instagram"
attribute is present and
1:55
matches the specified regular
expression for a valid URL.
1:58
The regular expression is generated
using the URI:: DEFAULT PARSER,
2:00
which ensures that the URL starts
with either "HTTP" or "HTTPs".
2:05
That's exactly perfect,
that's exactly what we want.
2:09
So we can add this, copy this line
of code and add it to the model, so
2:12
we're gonna understand models,
it's really cool.
2:16
Now, first of all, I just wanna
show you a different model, so
2:19
a different validation.
2:23
So, we're gonna say validates :instagram,
presence: true.
2:24
And for now that's okay, so
let's see what happens if I try and
2:29
create a presence: true, save the file.
2:34
Let's see what happens if I try and create
a tree without adding the Instagram.
2:37
It's not gonna work because we
have validated the Instagram, and
2:43
the presence has to be true.
2:46
So I'm gonna add this, add Instagram, but
I'm not actually gonna add it,I'm gonna
2:47
add YouTube instead, next,
light mode next, it shouldn't work.
2:51
And as you can see, it goes back to the
start and it says, one error prohibited
2:54
this tree from being saved, instagram
cannot be blank, so we need to fix that.
2:59
We need to add Instagram,
if we add Instagram, then click next, and
3:05
then add the mode again and click Next.
3:08
As you can see,
the tree was successfully created.
3:10
Don't worry,
3:13
there are just two Instagram here because
I added Instagram as the YouTube link.
3:14
Now let's go to a little bit more
of a complicated validation,
3:20
it's still pretty simple, but
it's still a bit more complicated.
3:24
So I'm gonna delete this, and
I've just added this if you just noticed,
3:28
I just added this, so that has to be
starting with the "HTTP" or "HTTPs".
3:32
So, now if I try and create a link tree,
and I add the Instagram,
3:36
but let's say I go something,
next, light mode next.
3:41
It's not gonna work because look,
it says instagram invalid URL format.
3:46
This is still quite a basic validation,
but
3:51
it's handy, so
I'm gonna do this for validates.
3:56
We're just gonna do it on the name
because we can't create the link tree
4:01
without a name.
4:06
So instead of that, we're gonna say name,
4:07
I actually forgot how
to add more characters.
4:12
So I'm gonna ask chatGPT,
so I'm gonna say,
4:17
validate that the name has
more than three characters.
4:21
You cannot keep everything in your head,
I'm sorry,
4:25
some things you're just gonna forget.
4:28
And as you can see, all you have
to do is say length, minimum four.
4:33
So, must be at least four characters long,
so I save this file, and
4:36
then I refresh, and then I try and create
a name with less than four characters,
4:41
and that's actually gonna give me a few.
4:47
So I'm gonna say mal, well,
I have that validation as well,
4:50
so i'll have to make this longer,
we'll say five.
4:56
So maIe,
we're gonna add the Instagram this time,
5:06
just so we don't get that error,
or that validation,
5:11
and next, name must be at
least four characters long.
5:16
Actually, that's the wrong message, we're
going to say five characters, but yeah,
5:21
that's correct,
that's exactly what we want.
5:26
So, now that you understand validations,
what they're about,
5:29
I want you to add validations to your
application to make it more secure.
5:33
All you have to do is add validates :name,
presence: true,
5:37
validates :instagram, things like that.
5:40
You can also validate images, so
if you want to validate the resolution of
5:43
an image, or the type of an image, you can
really validate anything, it's so cool.
5:47
So now that you understand validations,
5:51
we can get cracking onto the next topic,
which is the Device Edit User tab.
5:53
So I haven't talked about it yet,
but if we click my account,
5:58
it takes us to this Edit User tab, and
we need to be able to edit the email,
6:01
without inputting the password.
6:05
And that's what we're gonna do in
the next video So, yeah, let's get to it.
6:07
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up