Introduction to Rendering Collections via Partials in Rails 5
This introductory guide walks through how to leverage partials to automatically render collections of data. We'll also walk through when this approach works and when you will need to manually configure the process.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Looking at pivotal tracker we can see right here that we have three tasks that are left and two of them involve collections. One of them is for just pure basic data collections. The other one is one where we're going to be able to manually implement this feature. this is going to be what we cover in this guide.

let me open up Sublime text and I am going to close out all of these because we're not going to be using the layouts file in this guide. Open the index action for blogs and for portfolios. There is a very cool feature associated with Rails and how it can be very intelligent about rendering partials with collections. We can actually take out all of this content right here. Make sure you put an equal sign and say render blogs. If you come to the blogs directory, press new file and this is going to be a partial. It's going to blog _html.erb. We're doing a partial like we did with our nav bar. Now hit save.

large

large

You may wonder what in the world is this going to do, all we're saying is render blogs we're not calling partials, we aren't passing in any local variables and here we're actually calling a local variable. Let's see if this works

I'm going to open up the terminal and start the server, let's test if our blogs are still working. If they are, then we should have no difference in functionality. Click on blogs and our blogs are still showing up even though all of our code has been moved into this partial. All we're doing is saying I want you to render blogs. What exactly is happening here? This is something that's awesome about Rails and about how it can recognize certain patterns.

There is a very common term that you'll hear in regards to Rails development and it is called a convention over configuration.

That means that the rails developers follow a number of conventions and one of them is right here. Where you have a convention of being able to have a collection such as an index action, that simply takes in a collection. In this case of blogs, it iterates over them and then prints the values out. That happens so often that the developers who created Rail's said you know what we're just going to give this nice little shortcut syntax where developers are going to be able to say render blogs and then in the background Rails is going to look for the naming. Naming is very important, It's going to look for a partial called a blog and it's going to be singular.

If I would have named this underscore guides or even undercore blogs this would have thrown an error and it wouldn't have worked. In order for this to work, you have to pass in the instance variable and we get this from the index actions. Then Rail's said ok I see where you said Render blogs as an instance variable, I'm going to go look for a partial called blog. It finds it right here and then it does all of the iteration for us. Notice how we don't have an each block, the reason is, this process happens so many times and it's such a common thing to be able to take a collection and then show it on the screen.

If you've been working in Rails development for awhile then you know that that's a pretty common thing to do. If you haven't, then know that you're going to be performing this very similar action repeatedly. That's the reason why rails built it right into the framework itself. That is the way you can do that with a collection, that was with a scaffold.

Come down to portfolios and we have our index action right here, let's perform the exact same work but we don't have a table and that's perfectly fine. We can simply grab and delete this just like before. You can see portfolio item was in a block, in the backend of rails though it is actually doing this for us whenever we call this render collection process. Now we're going to do the same thing and say render portfolio items.

large

Here I can call portfolio, new file, hit save _portfolio_item.html.erb and paste all of this in. I'm going to give us a little space just so it's easier to read and this will give us everything that we need. I'm going to hit save and let's see if this works.

large

Come to my portfolio and oh it all broke, but not really. I want to show you why, and there is a very specific reason I wanted you to see this error. What I did really seems like it should have worked right? We did everything the same as we did with our blogs, we called render on blogs, we created a singular blog partial, we put all of the code that we wanted inside of it and that was all we needed to do. We created portfolio items and then we went and created a portfolio item partial, put it inside of the portfolios directory, everything should work but we're missing one step and that is the important part that I want to show you.

There is one other component that Rail's takes into account when it's building this parsing method. That is that it looks for the actual model controller name. Blogs is the controller name, blogs is the directory name, blogs is the plural version of the model. All of this is the exact naming structure. Now notice what we did on the portfolio item side, Portfolio items is not the name of the setup. Instead, this is portfolio items which is a name I arbitrarily gave. This does not mean that it's wrong though and that's a reason why I wanted to do this. There will be many times where you want to create a variable and you want to name it whatever you want. In this case, I named it like this because I don't like the word portfolios. I think it's bad to write poor English when you are writing a paper, it's also bad to write poor variable names. Other people are going to come try to read your code and if it doesn't read right there is a problem. I like the name portfolio items much better than just portfolios. Here, I like portfolio item instead of just being portfolio title and that kind of thing. What do we do? That is what we're going to actually cover in the next guide.

Notice how in pivotal tracker we have partials for data collections and then we have partials for manual collections. This is what we are talking about when I say manual collection. We can't tap into the Rails magic, we can't grab and just simply call this by itself. We have to give rails a few little hints on how exactly this is going to work. We're going to cover that in the next guide.

Resources