I have a callback url string params[:callback]
and I need to append a query string "&result=true"
and redirect the user. The better way I found of doing this is using addressable
but I think the code is too big for task like this especially when we are talking about ruby:
callback = Addressable::URI.parse(params[:callback])
query = callback.query_values
query[:result] = 'true'
callback.query_values = query
redirect_to callback.to_s
Is there a more elegant way of getting the same result as this snippet?
years later, I find a better solution of this problem.
Get the value from the
super
first, then do any tweaks we need usingAddressable
You can try with
merge
request.parameters.merge({:result => true})
this will add your parameter to the ones already defined.
I think you're pretty close to optimal. you could crush out a line or two, but it doesn't really gain you anything.
If the callback is always inside your application, you might have some other options with varying degrees of coolness, but you didn't specify if that was the case or not.
I wan't to bring update to this topic, because any of the solutions didn't work me.
The reason being, that it seems that
callback.query_values
returnsNil
if the actual url doesn't have existing query values.Therefore if you have urls such as:
http://www.foo.com
andhttp://www.foo.com?bar=1
you should use the following code:Cheers.