Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
provide a current_controller? helper useful in creating menu
# A rails helper snippet I find helpful for building main navigation
# when you want to highlight main pages (controllers) when browser
# requests a subpage.
# eg. current request '/users/1/edit' but we want to highlight
# the menu link to '/users'. current_page? will be false but
# the current_controller? function will be true
# Find if a link is uses the current controller.
# Used in building main navigation to include
# sublinks.
def current_controller?(link)
url_for(link).include? @controller.controller_name
# Create list elements for building navigation
def menu_link_li(text, link, classes = "", include_separator = false, new_tab = false)
if current_controller? link
classes += " selected"
rescue Exception => e
# deal with a potential error of not using the helper with a request first being made.
if new_tab
link_text = link_to text, link, :target => "_blank"
link_text = link_to text, link
html = %{<li class="#{classes}">#{link_text}</li>}
if include_separator
html += %{<li class="separator"></li>}
return html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment