Rendering Collections Reading

  1. Use the collection keyword with partials
  2. Pass a collection to the render method
  3. Handle empty collections

Overview

Up until now our only way to render collections was somewhat manually. We could iterate over an array and render the partial for each object in the array. Let's see how Rails can abstract this into a nicer syntax.

Lesson

Make sure you run rake db:seed before you test out the app in your browser. Because this lesson focuses on using the collection keyword with partials, we've hard-coded in the connection between authors and posts. In the posts controller create action, we link the newly created post with the first author in the database.

Rendering Collections

Currently, our posts#index view is manually rendering the partial in a loop.

<% @posts.each do |post| %>
  <%= render :partial => "post", {:locals => {:post => post}} %>
<% end %>

Rails offers a great way to render a collection using a partial by passing the collection option to the render method.

<%= render :partial => "post", :collection => @posts %>

Our code is tighter and both more abstract and more clear. Another even more abstract method Rails gives us to do this is passing an array directly to the render method.

<%= render @posts %>

This approach is a bit more abstract. Under the hood Rails uses the convention that you will have a partial with the name of the models in the collection. Rails will even render a collection of heterogeneous models ([customer, order, customer]) calling the correct partial for each one.

Empty Collections

What happens if the collection you pass to your render call is empty? If you don't handle this exception the render method will return nil and nothing will appear on the screen. A useful trick is to use the || operator to print something to the screen to alert the user to this.

<%= render(@posts) || "There are no blog posts!" %>

Note: When dealing with an empty collection, you'll need to use () to wrap that collection.

Conclusion

As always, Rails has tried to abstract commonly used functionality into more terse and implicit code. Experiment with these tricks in the upcoming lab.

Video Review

View Rendering Collections on Learn.co and start learning to code for free.

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us