Skip to content

Instantly share code, notes, and snippets.

@michaeldhopkins
Created January 12, 2012 01:28
Show Gist options
  • Save michaeldhopkins/1597894 to your computer and use it in GitHub Desktop.
Save michaeldhopkins/1597894 to your computer and use it in GitHub Desktop.
application.html.erb:
<%= generate_menu_link do %> <%= link_to "Home", :controller => "pages", :action => "show" %> <% end %>
application_helper.erb:
def generate_menu_link(&block)
link = with_output_buffer(&block)
li = ""
if is_link_to_current_page?(link)
li = '<li id="link-to-current-page">'
else
li = '<li>'
end
output = li + link + '</li>'
output.html_safe
end
def is_link_to_current_page? (link)
@current_url ||= request.fullpath
@current_url == link
link_url = link.match(/\bhref="[^"]*/)[0].gsub('href="','')
@current_url == link_url
end
Result if we're linking to the current page:
<li id="link-to-current-page"><a href="#">#</a></li>
Result if we're not:
<li><a href="#">#</a></li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment