Skip to content

19b Categories Show

Dave Strus edited this page Jul 16, 2015 · 1 revision

SOLUTION

The new partial varies just a little from the original contents of posts/index. First, we'll use the local variable posts in place of the instance variable @posts.

Second, because we're rendering this from outside the directory app/views/posts, we need to be more specific when rendering slug and actions.

New file: _app/views/posts/list.html.erb

<ul class="posts">
<% posts.each do |post| %>
  <li>
    <%= render 'posts/slug', post: post %>
    <%= render 'posts/actions', post: post %>
  </li>
<% end %>
</ul>

Back in posts/index, we'll render our new partial, assigning the required local variable.

app/views/posts/index.html.erb

<%= render 'list', posts: @posts %>

Then we'll render it in categories/show.

app/views/categories/show.html.erb

<% sidebar(@category.sidebar) -%>

<div class="category">
  <h2><%= @category.title %></h2>
  <div class="description"><%= @category.description %></div>

  <%= render 'posts/list', posts: @category.posts %>
</div>

If you haven't already, create a few catgories, and associate a few posts to each. If they look right, let's commit what we have.

You're still taking a look at git status and/or git diff before each commit, right?

$ git add .
$ git commit -m "Display posts by category."