[英]How do I specific creation of a link without params in rails?
在我看來,我想在頁面上時使用link_to_unless_current創建指向“ / payforms”的鏈接。 但是,當我傳遞參數時,例如“ / payforms?submitted = true”,該函數仍然認為我處於“當前”狀態,並且未創建鏈接。 有沒有辦法讓我檢查是否沒有參數?
<%= link_to_unless_current "All", payforms_path %>
也許是這樣的嗎?
<%= link_to_unless_current "All", payforms_path(:params => nil)
通常情況並非如此。
您可以通過以下方式檢查頁面是否相同
<%= 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?
一些提示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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.