如何在rails中没有params的特定链接创建?

时间:2010-06-24 18:58:56

标签: ruby-on-rails views

在我看来,我想在页面上使用link_to_unless_current创建一个指向“/ payforms”的链接。但是,当我传递参数时,例如“/ payforms?submitted = true”,该函数仍然认为我在“当前”并且没有创建链接。有没有办法检查是否没有参数?

<%= link_to_unless_current "All", payforms_path %>

也许是这样的?

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

1 个答案:

答案 0 :(得分:0)

一般来说,情况并非如此。

您可以通过

检查页面是否相同
<%= current_page?(payforms_path) %>

或通过

构建目标网址
<%= CGI.unescapeHTML(url_for(payforms_path)) %>

也许最后一个表达式也会返回'/ payforms / submitted'。

更新问题
您可以在application_helper.rb中声明自己的验证函数,然后将其传递给link_to_unless调用。

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

有关如何实施same_page?检查的一些提示,请参阅url_helper.rb中的current_page?。基本上,你只需要抛弃参数检查:

    # 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

- &GT;

    request_uri = request.request_uri