Q:

Assigning active class for bootstrap Nav-pills - Ruby on Rails

I have a blog section in my rails app and was wondering what a good way to handle assigning the CSS class active would be, based on the page that would be open.
Should this be done via .erb with if and do statements? It would need to reflect the category the post is in.
Blog Category Table:
create_table "blog_categories", force: :cascade do |t|
  t.string "name"
  t.integer "parent_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

Posts Table Column: t.integer "category_id" - This is stored  to relate to the .id of the cat.
My categories are represented with a parent_id of NULL and subcategories are those with a parent_id set to the .id of the main category.

A:

You can do this by defining a function in your application_helper.rb
def active_class(path)
 "active" if current_page?(path)
end

current_page? method will return active if the user is on the same url as passed in path parameter. checkout its documentation.
Then in view you can use it like this.
<li class="<%= active_class new_user_session_path %>">

