link_to_unless_current without request parameters

Not a really big thing, but I felt the need for a method like link_to_unless_current which ignores request parameters (you know, those things that come after the ‘?’ in the url). I couldn’t find it in the Rails api, so I wrote my own little snippet in a helper:


  # makes a link unless 'link' equals the current
  # page ignoring request parameters
  #
  # e.g. this will not make a link:
  #   current page = http://your.web.site/articles?page=2
  #   link = http://your.web.site/articles
  # but this will:
  #   current page = http://your.web.site/articles/2
  #   link = http://your.web.site/articles
  def link_to_unless_current_ignore_parameters(title, link)
    link_to_unless(request.request_uri.gsub(/\?.*/, '') == url_for(link), title, link)
  end

If someone knows a better way, please let me know :) I thought about aliasing the original method and having an optional boolean parameter that defines whether to ignore the request parameters, but decided to go with this instead.

Advertisements

2 Responses to link_to_unless_current without request parameters

  1. Tomasz Mazur says:

    Got this same problem lately, but decided to check whether controller and action matches…

    Your method is very cool, a think that in official method it should be option to disable GET params in this comparision

  2. I do taht, it’s work very cool.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: