Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Using Partials.docx58.8 KB Using Partials - Solution.docx
58.9 KB
Quick reference
Using Partials
In this video, we'll learn how and why to use partials.
When to use
Partials are especially useful when you have a block of code that you would like to separate out from other code. It keeps the original file clean and easy to read.
Instructions
To create a navbar partial, add a file to your app/views/home directory named _navbar.html.erb
Be sure to always name a partial with a preceding underscore _
To call a partial, use the following embedded Ruby code:
<%= render 'home/navbar' %>
Hints & tips
- Partial files should always be named with a preceding underscore ( _navbar.html.erb)
- <%= render 'home/navbar' %>
- Notice the 'home/navbar' text is NOT 'home/_navbar' even though we named our partial file _navbar.html.erb
- 00:04 In this video, I wanna talk about partials.
- 00:06 A partial is a way to add something from one file to another.
- 00:10 So we sorta have the same concept here in our application.html,
- 00:14 where we're adding something from one file to another.
- 00:18 But this yield is a unique case.
- 00:19 And we wanna be able to do something similar elsewhere,
- 00:22 besides on the application.html file.
- 00:25 So why would we wanna do that?
- 00:26 Well, we just created this navbar, right?
- 00:28 And there's all of this text.
- 00:31 And it's starting to make this application.html file kinda clunky.
- 00:35 So if we can get this out of here and put somewhere else and
- 00:38 just call it whenever we want to, that would be better.
- 00:41 So that's what we use partials for.
- 00:43 It's a little bit confusing, but as soon as we build one, you'll understand.
- 00:46 So the first thing to do is come up to our home directory and right-click.
- 00:49 And let's create a new file.
- 00:51 And when creating any partial, you always name it with an underscore.
- 00:56 That's just the convention for partials.
- 00:58 Now let's call this navbar.html.erb.
- 01:04 And let's come back to our application.html file, and
- 01:08 I'm just gonna highlight all of this navbar text.
- 01:12 And I'm gonna copy it, Ctrl+C.
- 01:15 And delete it.
- 01:17 And now I'm gonna open this navbar partial, and I'm just gonna Ctrl+V,
- 01:21 paste it in here, and save it.
- 01:23 So now all of our navbar text is in this navbar file.
- 01:27 Gotta save our application file, reload, suddenly that navbar, it disappeared.
- 01:31 So we wanna add this back.
- 01:33 So we come back to our application.html.erb file, and
- 01:37 let's add in the partial.
- 01:39 And to do that,we use embedded Ruby, just like always, opening tag and closing tag.
- 01:46 Now to add a partial, we type in render, and then in quotes,
- 01:52 we give it the directory and the name of the file.
- 01:56 In this case it's home/navbar.
- 01:59 And you'll notice, we named the file underscore navbar, but in this,
- 02:04 we didn't actually put underscore navbar,
- 02:07 you don't have to do that when you're creating a partial.
- 02:11 So let's save this, come back and hit reload.
- 02:13 And just like that, our navbar comes back.
- 02:17 Actually, let's get rid of this line break, too.
- 02:19 Save that and reload.
- 02:21 Okay, looks a little bit better.
- 02:23 So why is that cool?
- 02:25 For one thing, we have this application.html.erb file, now suddenly,
- 02:29 it's manageable again.
- 02:31 This is much easier to read than all of that gobblety gook we had before.
- 02:35 And also, in the future, if we want to add something to our navbar, and we will,
- 02:40 it's much easier just to come to the navbar file.
- 02:43 And add whatever you want, save it instead of rooting through the application file,
- 02:48 which is getting larger and larger and larger as we add more stuff to it.
- 02:51 It's just generally a cleaner way to code.
- 02:54 And Rails makes it so easy to use these partials.
- 02:57 We'll use them all the time and it's really cool.
- 02:59 So that's partials and that is all for this video.
Lesson notes are only available for subscribers.