2010-06-24 66 views
0

在我看来,我想使用link_to_unless_current在页面上创建“/ payforms”链接。但是,当我传递参数时,例如,“/ payforms?submitted = true”,该函数仍然认为我处于“当前”状态,并且不会创建链接。有没有办法让我检查是否没有参数?如何在rails中创建没有params的链接?

<%= link_to_unless_current "All", payforms_path %> 

也许是这样的?

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

回答

0

一般情况下,情况应该不是这样。

可以检查页面是一样的

<%= current_page?(payforms_path) %> 

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

也许最后表达构建目标URL返回 '/ payforms /提交' 太。

上更新的问题
你可以声明自己的验证功能,在application_helper.rb例如,然后把它传递给通话。

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

就如何落实same_page?检查,看current_page?在url_helper.rb一些提示。基本上,你只需要扔掉的参数检查:

# 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 
+0

其实,这个问题是不正确 - 我正在寻找一种方法,如果params为空。在rails中有这样的函数吗? – ehfeng 2010-06-24 19:59:13

+0

它工作!非常感谢。 – ehfeng 2010-06-24 22:17:08