Rails foundationSort Order

Rails: Clean up the Posts Index Page

In this episode we clean up the posts index page. We take a look at some concepts behind bootstrap's grid system and use it to clean up the alignment of elements on our page. We also move some code into their own partials, and write our first helper method.

We'll start off by moving the sign in / sign up buttons out of the layout.

# app/views/application/_signed_in.html.erb<li>  <%= link_to current_user.email, '#' %></li><li>  <%= link_to 'Sign out', session_path, method: :delete %></li>
# app/views/application/_signed_out.html.erb<li>  <%= link_to 'Sign in', new_session_path %></li><li>  <%= link_to 'Sign up', new_user_path %></li>

We also need another partial that will wrap the _signed_in and _signed_out partials we just created. We'll call the helper method render_user_auth_nav in this partial.

# app/views/application/_navbar.html.erb<nav class='navbar navbar-default navbar-static-top'>  <div class='container'>    <div class='navbar-header'>      <%= link_to 'Writefully', '#', class: 'navbar-brand' %>    </div>    <ul class='nav navbar-nav navbar-right'>      <%= render_user_auth_nav %>    </ul>  </div></nav>

We also need to implement the logic that will decide which partial to render, we'll do that in the application_helper.rb

# app/helpers/application_helper.rbmodule ApplicationHelper  def render_user_auth_nav    if current_user      render 'signed_in'    else      render 'signed_out'    end  endend

This episode has a few other clean up of the views we recommend watching the video to see all the changes that took place. The code above should give you an idea of some of the changes we're making to our codebase.