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 Rails 8 Course - Beginner to Intermediate!
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
This video provides a detailed overview of how Rails manages posts through its views, routes, controllers, and partials—explaining the dynamic rendering of Ruby code and setting the stage for adding user authentication.
Resources
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
Now the next main thing that we
have on the agenda is to create
0:00
an ad user authentication.
0:02
But before we do that, I just want to
talk a little bit about the posts and
0:04
how it all works, because I think I need
to give you a good understanding if you
0:07
haven't got one already.
0:11
If you have got one,
this would be a good refresher.
0:12
So it's gonna be pretty basic.
0:14
So if we go to App, Views, Posts, Index,
index.html.erb, this is the main page.
0:15
This is this page right here.
0:21
You know like HTML, this is that page, but
0:23
it has Ruby code in it
because it's a .html.erb.
0:25
Now the reason that we're on this
page when we start up the server and
0:28
we go to the URL, like even when we go
to this page, that's still this page.
0:31
Slash posts on this page and
the root URL is the same thing.
0:35
So the reason that we go to this
page when we start up server and
0:38
we go to the application is
because if we go to routes and
0:41
I do Ctrl+P to go to Routes or
you can just go to Config and
0:44
then routes.rb and
that's because we said root post#index.
0:47
Now this is extremely important.
0:51
This is the fundamental
way of how Rails works.
0:53
So root post#index means,
means go to the views post index and
0:57
render that one as the root file.
1:01
Something else that's
a little bit unrelated
1:04
is that post controller index
is linked to this index file.
1:07
So that means that any variable
that we have in the index method,
1:12
it's called,
is linked to this index file here.
1:16
Now the post controller, all of these
methods, they're usually not all the time,
1:20
but they usually are by default
their own separate pages.
1:24
So for example, in new there's a new
page if we go to post controller and
1:27
then there's show,
there's also a show page.
1:31
I just want you to know that
these methods and these pages,
1:33
these different files are linked, okay?
1:35
So when we go to new and let's say we want
to add a variable in let's say we want
1:37
a variable,
then we'd like pass it into new and
1:41
then that's how that would work and
we display on this page.
1:44
So I just want you to understand also
something else is that when we go to
1:47
new post it's on on the index, right?
1:52
If we go back run the index,
we click New post.
1:54
So that's this link to right here.
1:57
So link to and then the text and
the new post path.
1:59
I'll teach you something
else now in a second.
2:02
But let's say we go to new post and
then as you can see,
2:03
there's a form rendered here.
2:06
Now if we go to new,
it says render form, post @post.
2:08
Now where actually is this form?
2:11
What actually is it?
2:13
Well, this is called a partial.
2:13
And a partial is essentially
a small amount of code,
2:15
a bundle that you can
just have somewhere else.
2:17
And it's actually in this post folder and
that's why it just says form.
2:20
As you can see, it says _form.html.erb.
2:23
And in here we have all the actual code,
it says form.textarea :title, form.label,
2:25
all that good stuff.
2:30
So this is where the form actually is.
2:31
This is an actual page that we can go to,
right?
2:33
We're on this page right now and
2:34
it renders this other bit
of code onto the page.
2:36
Now when we actually go to create a post,
so I'll just say this is the first
2:38
post and we create the post,
it goes to the show page, right?
2:43
And inside of the show page we have
another partial, it says render @post.
2:47
I'm just giving you an overview
of how this actually works.
2:51
So render @post, again,
this is the same thing as before.
2:54
Same thing as the form,
it's called a partial.
2:58
And if we go to _post.html.erb,
this is where the post code actually is.
2:59
So if I wanted to change this title to be
in bold, I would wrap it in strong, so
3:05
might actually put it in there.
3:10
And as you can see now it's bold, right?
3:12
Because this is essentially just this line
of text because that's the whole post.
3:14
But if there was more like if we
had a user ID or a description,
3:20
then that would be there too.
3:22
Now something else that's also quite
important, if I just bump this up so
3:24
you can see these Ruby signs,
this is a Ruby sign, right?
3:27
So it goes less than bracket and
then percentage sign and
3:30
then percentage sign and
then greater than bracket.
3:33
Now if you want to render
something onto the page, or
3:35
if you want to show it onto the page,
then you have to have an equal sign, okay?
3:38
So you have to have an equal sign
after the first percentage sign.
3:41
So make sure to understand this because
that means that it's Ruby code.
3:44
And if you have an equal sign,
that means that it's shown on the page.
3:47
If you don't have an equal sign, then that
means that it's not shown on the page.
3:50
And if you have a hashtag,
then that means that it's a comment.
3:53
So, yeah, that's a brief overview
of how this whole thing works.
3:56
And this is really important, you have to
understand all of this, mainly because if
3:59
you want to actually style anything and
make it look a little bit better,
4:03
then you need to understand
where things are, right?
4:07
So if we wanted to change the button
on this button right here, this post,
4:10
we wanna change the color,
then we'd have to go to the index, and
4:13
right here, this is the link.
4:16
We'd have style, and this syntax,
we'd have comma, style, and then colon.
4:18
And then we just change the color and
we could say, color gold should work.
4:24
And now you can see it's gold, right?
4:29
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