views:

21

answers:

1

In my view, I want to use link_to_unless_current create a link to "/payforms" when I'm on page. However, when I pass parameters, such as, "/payforms?submitted=true", for example, the function still thinks I'm on "current" and the link is not created. Is there a way for me to check if there are no parameters?

<%= link_to_unless_current "All", payforms_path %>

Maybe something like this?

<%= link_to_unless_current "All", payforms_path(:params => nil)
A: 

Generally, it shouldn't be the case.

You can check if page is the same by

<%= current_page?(payforms_path) %>

or construct target url by

<%= CGI.unescapeHTML(url_for(payforms_path)) %>

Probably last expression returns '/payforms/submitted' too.

on updated question
You can declare your own validation function, in application_helper.rb for example, and then pass it to link_to_unless call.

<%= link_to_unless same_page?(payforms_path), "All", payforms_path %>

Some hints on how to implement same_page? check, see current_page? in url_helper.rb. Basically, you just need to throw-away parameters check:

    # 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
    else
      request_uri = request.request_uri.split('?').first
    end

-->

    request_uri = request.request_uri
Nikita Rybak
Actually, the question was incorrect - I'm looking for a way to see if params is empty. Is there a function like this in rails?
ehfeng
It worked! Thanks so much.
ehfeng