Rails foundationSort Order

Rails: Clean up the Individual Post

We clean up the individual post page, by applying our grid system to the application layout. This means we can simplify some of the existing code.

Cleaning up the Layout

In the app/views/layouts/application.html.erb we can add the div to put everything in the grid system.

<div id='main' class='container'>  # remove  <% if current_user %>  <% else %>  <% end %>    # add  <div class='row'>    <div class='col-md-8 col-md-offset-2'>      <%= yield %>    </div>  <% end %></div>

Cleaning up the Post Partial

Next thing we can now do is remove some classes from the _post.html.erb partial.

# from<%= div_for post, class: 'col-md-8 col-md-offset-2' do %> # ...<% end %># to<%= div_for post do %> # ...<% end %>

Moving the Comments into their own Partial

In the app/views/posts/show.html.erb page we need to move the comments out into it's own partial. Let's create a file app/views/comments/_comments.html.erb

We want to move this part out into the new partial.

<hr><h2>Comments</h2><% @post.comments.each do |comment| %>  <div class='panel panel-default'>    <div class='panel-body'>      <%= comment.body %>      <div class='author'>        Commented by: <%= comment.user.email %>      </div>    </div>  </div><% end %><%= form_for [@post, @post.comments.build] do |f| %>  <div class='form-group'>    <%= f.text_area :body, class: 'form-control' %>  </div>  <div class='actions'>    <%= f.submit "Save", class: 'btn btn-success' %>  </div><% end %>

Then in the app/views/posts/show.html.erb should now look something like this.

<h1><%= @post.title %></h1><p><%= @post.body %></p><%= render 'comments/comments' %>

That's a wrap for this episode!