I have this url:
http://localhost:3000/blog/posts?locale=en
I have a helper to remove ?locale=en
of url:
def url_without_locale_params(url)
uri = URI url
params = Rack::Utils.parse_query uri.query
params.delete 'locale'
uri.query = params.to_param
uri.to_s
end
With this helper I get this url http://localhost:3000/blog/posts?
. I would like to delete the trailing ?
.
The result should be http://localhost:3000/blog/posts
.
Use #gsub:
uri = "http://localhost:3000/blog/posts?locale=en"
uri.gsub(/\?.*/, '')
#=> "http://localhost:3000/blog/posts"
String#chomp
is a possibility.
1.9.3p392 :002 > "foobar?".chomp("?")
=> "foobar"
The final method will be
def url_without_locale_params(url)
uri = URI url
params = Rack::Utils.parse_query uri.query
params.delete 'locale'
uri.query = params.to_param
uri.to_s.chomp("?")
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With