Posted By

obsessivejosh on 07/30/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


Highlight Section Navigation


 / Published in: Rails
 

URL: http://snippets.dzone.com/posts/show/2016

note to self

  1. #put in application_helper.rb :
  2.  
  3. def section_link(name,options)
  4. if options[:action] == @current_action and options[:controller] == @current_controller
  5. link_to(name, options, :class => 'on')
  6. else
  7. link_to(name,options)
  8. end
  9. end
  10.  
  11. #initialize vars in controller
  12. before_filter :instantiate_controller_and_action_names
  13.  
  14. def instantiate_controller_and_action_names
  15. @current_action = action_name
  16. @current_controller = controller_name
  17. end
  18.  
  19. #usage in view:
  20. <%=section_link('Home',:controller => 'articles', :action => 'index')%>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: incognick on May 7, 2010

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)
link
to(name, options, :class => 'on') else link_to(name,options) end end

This allows for RESTful links as well:

You need to login to post a comment.