簡體   English   中英

Rails - 獲取沒有GET參數的當前URL

[英]Rails - Get the current url without the GET parameters

request.url返回給我: http:// localhost:3000 / page?foo = bar

有沒有我可以調用的方法來獲取http:// localhost:3000 / page ,還是我必須解析字符串以去除get參數?

如果您不關心主機名, request.path應該返回您要查找的內容。 否則你可能會嘗試:

url_for(:only_path => false, :overwrite_params=>nil)

獲取沒有任何查詢參數的請求URL。

def current_url_without_parameters
  request.base_url + request.path
end

我使用以下內容:

request.original_url.split('?').first

它不會重新生成路徑,因此會為您提供最終用戶在瀏覽器中看到的URL,減去查詢參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM