MrRuru
MrRuru

Reputation: 1932

Rails - Get the current url without the GET parameters

request.url returns me this : http://localhost:3000/page?foo=bar.

Is there a method I can call to get http://localhost:3000/page , or do I have to parse the string to strip get parameters?

Upvotes: 43

Views: 40249

Answers (4)

Rupcio
Rupcio

Reputation: 67

You can use full_url_for - this omits any query params from the URL.

eg. for http://example.com/foo?bar=1234 it returns http://example.com/foo

https://apidock.com/rails/ActionDispatch/Http/URL/full_url_for/class

Upvotes: 1

Nerve
Nerve

Reputation: 6901

To get the request URL without any query parameters.

def current_url_without_parameters
  request.base_url + request.path
end

Upvotes: 18

thekingoftruth
thekingoftruth

Reputation: 1781

I use the following:

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

It does not regenerate the path, and therefore gives you exactly the url that the end-user sees in their browser, minus query parameters.

Upvotes: 13

tadman
tadman

Reputation: 211740

request.path should return what you're looking for if you're not concerned about the hostname. Otherwise you might try:

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

Upvotes: 59

Related Questions