Slightly different content many Rails views - how to be DRY -
hopefully haven't duplicated question. it's similar this one , this one not quite.
on subject of duplication, problem. i'm new rails btw.
i have (twitter bootstrap) set of pills/tabs , want <li> class set "active" if user on page associated tab. far i'm doing stupid way.. in each of views have (simplified):
<% content_for :pills %> <li><a href="/">home</a></li> <li class="active"><a href="/about">about</a></li> <----------- active class <li><a href="/contact">contact</a></li> <% end %> and in each view have same code (doh) class set "active" on tab/pill.
most tab content comes same app/views/<some view directory>/ 1 tab comes app/views/<some other view directory>/ guess isn't problem.
how solve without repeating similar code in each view?
many thanks.
i've run same thing , created helper i've used in few projects.
add app/helpers/layout_helper.rb file.
module layouthelper def nav_class_for(*controllers) :active if controllers.any? |controller| controller_name.to_sym == controller.to_sym || params["#{controller.to_s.singularize}_id"] end end end then use this.
<li class="<%= nav_class_for(:about) %>"><a href="/about">about</a></li> just pass name of controller or controllers want menu item active for. should active nested routes beneath controllers.
Comments
Post a Comment