/ Published in: Rails
URL: http://snippets.dzone.com/posts/show/2016
note to self
Expand |
Embed | Plain Text
#put in application_helper.rb : def section_link(name,options) if options[:action] == @current_action and options[:controller] == @current_controller link_to(name, options, :class => 'on') else link_to(name,options) end end #initialize vars in controller before_filter :instantiate_controller_and_action_names def instantiate_controller_and_action_names @current_action = action_name @current_controller = controller_name end #usage in view: <%=section_link('Home',:controller => 'articles', :action => 'index')%>
Comments
Subscribe to comments
You need to login to post a comment.

Thanks! I looked all over for something simple like this. I couldn't get RESTful routes working with yours so I kept digging.
There is a helper in ActionView::Helpers::UrlHelper called current_page?(options) [http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html]
def sectionlink(name,options = {}, htmloptions = nil) if currentpage?(options)
linkto(name, options, :class => 'on') else link_to(name,options) end end
This allows for RESTful links as well: