Guide to Manually Configuring Partials to Work with Collections in Rails
In the last guide we implemented an automated way to render collections in a Rails view template. In this guide we'll examine how we can manually configure collections to work with partials.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In pivotal tracker, one thing I've forgotten the last guide was to get complete on partials for data collection. Now we're going to talk about how we can implement partials for manual collections. This is going to be something that is going to let us finish what we started in the last guide. I did not push up anything to GitHub for the last guide and that was on purpose, Usually, you don't want to push things up that still have errors in them. That's just kind of a standard practice. You want your commits to be segmented and give you benchmarks in time where things are working even if they're not working exactly the way that you want or if you haven't refactored. That's why I didn't push anything up on that.

Switching back here. I left everything open and I want to take a quick review on what we did and why this is not working. This is very important, you're going to be following this type of pattern quite a bit. It's important to understand when it will work and when it won't. Now our blog index template we're able to simply say render blogs and everything happens seemingly by magic.


We all know it's just Ruby code but still it's pretty cool how I can just call render and pass in the instance variable and then the entire system works. Then we can pass our regular partial type of style here with the and then Rails has the "each" iterator which iterates over, passes in an instance variable and everything simply works. The reason why that's possible is because of the naming structure. The naming matches perfectly, there's a term for this, this is considered the rails way of doing things and so that means that we're following a very strict naming convention. When you do that you have access to very cool kinds of methods. This works in a number of instances but it doesn't always work and that's what I want to show you.

Our portfolio items is going to show an error right here and it says ActionView::MissingTemplate. What exactly is missing? Let's take a look. It says missing partial portfolios/portfolio with these data items but that's kind of weird because why is it looking for portfolio. We said portfolio items, this gives us a little glimpse into the way that the rails parsing engine works. It is going to look at the name and it's going to follow the naming pattern that is in this directory, that's in the controller and it's going to simply assume that we're trying to do what we did with blog. Rails has no way of knowing that I wanted to switch the name of the instance variable to portfolio items, we have to tell it. That's what we're going to do here.

After render, we have to manually declare that this is a partial. I'm going to say render partial and then I put in the partial name. I'm going to say portfolio item than a comma and then I'd say collection. We've manually declared this as a partial. It will not work without this. We're saying this is a partial and we're passing in the exact name of the partial. Notice we didn't have to go and say portfolios/portfolio item, it's smart enough to know that if we're rendering one of these collection based systems, it's going to be inside of the directory. If you wanted to do outside you still could do that but if you pass a directory, it's going to assume that it is right inside here. This collection is telling rails this is a collection. We're having to manually say by the way this type of render in this type of partial is going to be of this collection type. We passed that in and then we simply pass the instance variable, the cool thing is we're not going to have to make any changes right here. Everything here should be able to stay the same.


Come up, hit refresh and everything is now working. If I hit create new item everything here works all of my other pages work. Our blog page still works Everything is good on that side.

That's an important thing and I wanted to split up and I wanted you to be able to understand the difference. Part of why I split it up was I want you to be able to use it as a reference material item in the future. By splitting it up I am able to name one a standard collection type of partial call and the other one will be a manual collection. When you look it up you'll be able to access it and reference it very quickly.

That is how you can manually configure a collection based partial and be able to get pretty much all the same type of functionality that we got when we did it automatically. Notice here how we didn't have to put each and do and pass in a regular local variable by putting collection here. By declaring all of this, that did all of the work for us. That makes it just much easier than having to do all that yourself.

  • status
  • git add .
  • git commit -m "Implemented collection based partials"
  • git push origin view

Now we can come and we can actually close off this partials for manual collections.

Great job if you went through this, in the next guide we are going to talk about some very cool view helper methods.