Search code examples
ruby-on-railsroutesactionviewactionviewhelper

Passing path to current_page method


I have a helper method to help to determine whether or not a navigation menu item should be active/inactive.

  def events_nav_link
    nav_item = 'Events'

    if current_page?(events_path) # additional conditions here
      # do nothing
    else
      nav_item = link_to nav_item, events_path
    end

    content_tag(:li, nav_item + vertical_divider, :class => 'first')
  end

I want this link to be inactive not only for the events_path page, but for other pages as well. Problem is that I don't know what param to pass the current_page?(path) method based on this route:

map.list_events '/events/:league/:year/:month/:day', :controller => 'events', :action => 'list'

This route generates a path such as /pow or /chow/2011/09/25. Leagues can be pow, chow or wow.


Solution

  • I like unobtrusive JS approach with add/remove classes and unwrap() deactivated links, but it requries specific rules.

    The Rails way is to use link_to_unless_current built-in helper to highlight and unlink on current page href.