## Refactoring
Now that we have Posts and Comments working, if we take a look at the `app/views/posts/show.html.erb` template, it’s getting long and awkward. We can use partials to clean this up.
### Rendering Partial Collections
First we will make a comment partial to extract showing all the comments for the post. Create the file `app/views/comments/_comment.html.erb` and put the following into it:
@@@ html
Commenter:
<%= comment.commenter %>
Comment:
<%= comment.body %>
@@@
Then in the `app/views/posts/show.html.erb` you can change it to look like the following:
@@@ html
<%= notice %>
Name:
<%= @post.name %>
Title:
<%= @post.title %>
Content:
<%= @post.content %>
Comments
<%= render :partial => "comments/comment",
:collection => @post.comments %>
Add a comment:
<%= form_for([@post, @post.comments.build]) do |f| %>
<%= f.label :commenter %>
<%= f.text_field :commenter %>
<%= f.label :body %>
<%= f.text_area :body %>
<%= f.submit %>
<% end %>
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %> |
@@@
This will now render the partial in `app/views/comments/_comment.html.erb` once for each comment that is in the `@post.comments collection`. As the render method iterates over the `@post.comments` collection, it assigns each comment to a local variable named the same as the partial, in this case comment which is then available in the partial for us to show.
### Rendering a Partial Form
Lets also move that new comment section out to it’s own partial, again, you create a file `app/views/comments/_form.html.erb` and in it you put:
@@@ html
<%= form_for([@post, @post.comments.build]) do |f| %>
<%= f.label :commenter %>
<%= f.text_field :commenter %>
<%= f.label :body %>
<%= f.text_area :body %>
<%= f.submit %>
<% end %>
@@@
Then you make the `app/views/posts/show.html.erb` look like the following:
@@@ html
<%= notice %>
Name:
<%= @post.name %>
Title:
<%= @post.title %>
Content:
<%= @post.content %>
Comments
<%= render :partial => "comments/comment",
:collection => @post.comments %>
Add a comment:
<%= render "comments/form" %>
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %> |
@@@
The second render just defines the partial template we want to render, `comments/form`, Rails is smart enough to spot the forward slash in that string and realize that you want to render the `_form.html.erb` file in the `app/views/comments` directory.
The `@post` object is available to any partials rendered in the view because we defined it as an instance variable.