Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Addressable::URI – get url without any parameter

url = Addressable::URI.parse("https://my.url?with=a&lot=of&parameters")

How do I get the base url without ANY query parameter, without knowing which parameters or how many there are?

I can’t do url.query_values = {} because I also need the original url at some point.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What you want is to use omit:

Addressable::URI#omit(*components) ⇒ Addressable::URI

Omits components from a URI.

url = Addressable::URI.parse('https://sub.example.com/path/to/foo?x=1&y=2#hash')

url.omit(:query).to_s
# =>
"https://sub.example.com/path/to/foo#hash"

If you also don’t want the hash or other components, pass them to omit as well:

url.omit(:query, :hash)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading