This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
"Partial" is short for "partial view". And it's a partial that contains the HTML code for the form.
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
So in both our edit and
0:00
new templates, we see a call to the render
method with an argument of form.
0:02
When render is called from either of
these templates, it looks up a partial.
0:06
Partial is short for PartialView and
0:11
it's this partial contains
the HTML code for the form.
0:14
When you need an element to look identical
on multiple pages in your app, it's a pain
0:18
to maintain multiple copies of that
element within multiple erb templates.
0:23
Any time you change one,
you have to remember to change the other.
0:28
Otherwise, they could get out of sync and
might wind up looking different or
0:31
one of them could stop working altogether.
0:35
And that's why Rails offers partials.
0:38
You can take the HTML code that's
identical between the templates and
0:41
move it to a partial view.
0:45
Then, each template that needs that
code can render the partial and
0:47
the partial's code will be
included in the template's code.
0:50
When you need to make a change,
you only have to update the partial.
0:54
Any changes you make will immediately
be visible in all the views that
0:57
use that partial.
1:01
So both the Edit Post page and
1:03
the New Post page include a call
to render a partial called form.
1:04
If we look at the log,
1:09
we'll see it's been showing us the step
where it renders the partial all along.
1:10
Within both the edit and new requests,
we'll see rendered posts/_form.html.erb.
1:14
The underscore at the start of
the filename indicates that a file
1:21
holds a partial template.
1:24
When calling render, you can leave the
underscore and the html.erb extension off.
1:26
Rails adds those automatically,
that just leaves render form.
1:32
Following the name of the partial is
a hash that sets up local variables for
1:36
use within the partial.
1:39
So this will make the object referenced
by the post instance variable
1:41
available within the partial
under the post local variable.
1:45
Let's open the partial file
to see what it contains.
1:49
It's at app > views >
posts > _form.html.erb.
1:53
We can see that it takes
the post object we passed in and
2:00
renders an HTML form for it.
2:03
The f block parameter here will hold the
Ruby object representing the HTML form.
2:06
We can call methods on that object
to add elements to the HTML form.
2:11
We can see the code for
the title field here.
2:16
Let's mimic that for the body field.
2:21
So we'll copy it and paste.
2:23
We'll create another div
with class of field.
2:28
We'll set up an HTML label for
the body field.
2:32
Just like the one for the title by calling
the label method on the form object and
2:36
passing it a parameter of body.
2:41
Then we'll set up a field for
the body, but
2:43
instead of calling the text field
method we'll call the text area method.
2:46
This will give the user
a larger area to type in.
2:51
And we'll change the title
argument to body.
2:55
If we save our work,
go back to our browser and
3:00
refresh our page, we'll see a new
form where we can enter a post body.
3:03
And if we go to the Edit page,
we'll see a field there as well.
3:08
But there's a problem.
3:13
On our New Post form,
if we fill up the post body and submit it,
3:15
We'll see that the body
field is still blank.
3:22
To fix that, we're going to have to make
one last change in our controller code.
3:25
We'll look at how to do that next.
3: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