Append query string to url

Viewed 17679

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?

7 Answers

Let me offer this one modestly. I suggest using only strings for query parameters keys and values (like Arye noted) . Also, NilClass instances have a to_h method, which allows to remove some brackets:

callback = Addressable::URI.parse(params[:callback])

callback.query_values = callback.query_values.to_h.merge("result" => "true")

redirect_to callback.to_s
Related