Created
January 12, 2012 01:28
-
-
Save michaeldhopkins/1597894 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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