2010-06-24 27 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