ruby on rails - link helpers, current_pages and navigation -
i'm trying keep tab on page selected checking controller. i.e., page displays products
controller should keep product
tab selected. can't seem find right approach. considering of making helper method, seems bit tricky put helper link_to
helper. here guess of how can make work:
<%= link_to "products", products_path, current_controller?('products') ? :class => 'selected' %>
anyone has better idea?
and problem wont in 1 place, you'll have many tabs , each tabs have rules on controllers+action combinations active/selected for.
it's common problems , people have written "plugins" same too.
i suggest write helpers. make own mini dsl. decide easy , nice at:
<%= link_to_tab_for("products", products_path, :controller => "sss", :action => "", :other_html_options => {})
next step, implement method in helpers/application.rb
def link_to_tab_for(name, path, options) controller = options.delete(:controller) action = options.delete(:controller) klass = [].push(options[:class]).compact if current_controller?(controller) && (action ? current_action?(action) : true) klass.push("selected") end options[:class] = klass.empty ? "" : klass.join(" ") link_to(name, path, options) end
have gander @ attempting above method liking of course.
Comments
Post a Comment