Removing a part of a URL with Ruby

query-string, ruby, ruby-on-rails, url

Solution

 url = 'http://www.domain.extension/folder?schnoo=schnok&foo=bar'
 u = URI.parse(url)
 p = CGI.parse(u.query)
 # p is now {"schnoo"=>["schnok"], "foo"=>["bar"]}

Take a look on the : how to get query string from passed url in ruby on rails

Problem

Removing the query string from a URL in Ruby could be done like this: ``` url.split('?')[0] ``` Where url is the complete URL including the query string (e.g. `url = http://www.domain.extension/folder?schnoo=schnok&foo=bar`). Is there a faster way to do this, i.e. without using split, but rather using Rails? edit: The goal is to redirect from `http://www.domain.extension/folder?schnoo=schnok&foo=bar` to `http://www.domain.extension/folder`. EDIT: I used: ``` url = 'http://www.domain.extension/folder?schnoo=schnok&foo=bar' parsed_url = URI.parse(url) new_url = parsed_url.scheme+"://"+parsed_url.host+parsed_url.path ```

Original source

Related problems