Nav_link_to update
Now it also detects matching controller, so the link will be marked as 'selected' for /posts, /posts/id, /posts/new etc.
https://gist.github.com/firedev/5308768
# Provides a `nav_link_to` view helper method that mimics `link_to` # but adds `selected` CSS class if the link equals current page or handled by the same controller. # Put into `intializers`. # Provides a `nav_link_to` view helper method that mimics `link_to` but adds a `selected` class if the link equals current page. # Put into `intializers`. module ActionView module Helpers module UrlHelper def nav_link_to(name = nil, options = nil, html_options = nil, &block) html_options, options = options, name if block_given? options ||= {} if current_page?(options) || Rails.application.routes.recognize_path(options)[:controller] == params[:controller] html_options ||= { } html_options[:class] = '' unless html_options[:class] html_options[:class] << ' selected' end html_options = convert_options_to_data_attributes(options, html_options) url = url_for(options) html_options['href'] ||= url content_tag(:a, name || url, html_options, &block) end end end end











