public Method

UrlHelper.link_to_unless_current(name, options = {}, html_options = {}, &block)

Creates a link tag of the given name using a URL created by the set of options unless the current request URI is the same as the links, in which case only the name is returned (or the given block is yielded, if one exists). You can give link_to_unless_current a block which will specialize the default behavior (e.g., show a "Start Here" link rather than the link’s text).

Examples

Let’s say you have a navigation menu…

<ul>
  <li><%= link_to_unless_current("Home", { :action => "index" }) %></li>
  <li><%= link_to_unless_current("About Us", { :action => "about" }) %></li>
</ul>

If in the "about" action, it will render…

<ul>
  <li><a href="/controller/index">Home</a></li>
  <li>About Us</li>
</ul>

…but if in the "home" action, it will render:

<ul>
  <li><a href="/controller/index">Home</a></li>
  <li><a href="/controller/about">About Us</a></li>
</ul>

The implicit block given to link_to_unless_current is evaluated if the current action is the action given. So, if we had a comments page and wanted to render a "Go Back" link instead of a link to the comments page, we could do something like this…

<%=
    link_to_unless_current("Comment", { :controller => 'comments', :action => 'new}) do
       link_to("Go back", { :controller => 'posts', :action => 'index' })
    end
 %>

Source Code

# File action_view/helpers/url_helper.rb, line 270
def link_to_unless_current(name, options = {}, html_options = {}, &block)
  link_to_unless current_page?(options), name, options, html_options, &block
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.