request.url返回我這個:http://localhost:3000/page?foo=bar。Rails - 獲取沒有GET參數的當前url
有沒有一種方法,我可以打電話來獲得http://localhost:3000/page,或者我必須解析字符串去掉get參數嗎?
request.url返回我這個:http://localhost:3000/page?foo=bar。Rails - 獲取沒有GET參數的當前url
有沒有一種方法,我可以打電話來獲得http://localhost:3000/page,或者我必須解析字符串去掉get參數嗎?
request.path
如果您不關心主機名,應該返回您正在查找的內容。否則,你可以嘗試:
url_for(:only_path => false, :overwrite_params=>nil)
我使用以下命令:
request.original_url.split('?').first
它不會重新生成路徑,因此給你確切的最終用戶看到在其瀏覽器的URL,減去查詢參數。
不錯! terser request.url.split('?')。first'也適用於我。 – Franco
@Franco我相信'request.url'已被棄用,但我不確定。 – thekingoftruth
這更靈活,因爲它也可以使用'request.referrer'。 – steel
獲取沒有任何查詢參數的請求URL。
def current_url_without_parameters
request.base_url + request.path
end
我也遇到了遞歸參數問題'/ controller/action?param = 1?param = 2?param = 3',但request.path對我來說工作得很好。 –
上述方法的問題是,如果存在多個相同類型資源的路徑(例如對於某些事物標記爲「/ news/post-name」等的出版物「/ blog-name/post-name」 ),那麼你可能會得到錯誤的道路。 – thekingoftruth
@thekingoftruth這是真的,但最高優先級的路線將被使用,所以一定要正確命令它們。 – tadman