Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Having a quick look at the source for <code>link_to_unless_current</code>...</p> <p>...it makes use of <code>current_path?</code> such that you should be able to do something like this:</p> <p>In a helper... </p> <pre><code>def current_page_in?(*pages) pages.select {|page| current_page?(page)}.compact.any? end </code></pre> <p>... and then in your view, you can just supply an array of either named_routes or hashes like Shadwell's answer above.</p> <pre><code>&lt;%= link_to_unless(current_page_in?(new_thing_path, things_path), "add a thing") %&gt; </code></pre> <p>You get the idea...</p> <p>UPDATED</p> <p>Had a think about this... and it'd be great if you could just use it like you'd hoped that the original method worked. Here we compare the supplied named route (or controller + action hash) with the current page AND its referrer.</p> <pre><code>def current_page_or_referrer_in(options) url_string = CGI.unescapeHTML(url_for(options)) request = @controller.request # We ignore any extra parameters in the request_uri if the # submitted url doesn't have any either. This lets the function # work with things like ?order=asc if url_string.index("?") request_uri = request.request_uri referrer_uri = request.referrer else request_uri = request.request_uri.split('?').first referrer_uri = request.referrer.split('?').first end #referrer_uri always has full path (protocol, host, port) so we need to be sure to compare apples w apples if url_string =~ /^\w+:\/\// ["#{request.protocol}#{request.host_with_port}#{request_uri}", referrer_uri].include?(url_string) else referrer_uri = referrer_uri.gsub(request.protocol, '').gsub(request.host_with_port, '') [request_uri, referrer_uri].include?(url_string) end end </code></pre> <p>The beauty is that it now lets you just do this (from your example):</p> <pre><code>&lt;%= link_to_unless(current_page_or_referrer_in(new_task_path), "Add a task") %&gt; </code></pre> <p>It'll then display if you're on <code>new_task_path</code> OR a page to which it has been sent (such as the create page</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload