Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 707 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append query string to url

#1
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?
Reply

#2
years later, I find a better solution of this problem.

Get the value from the `super` first, then do any tweaks we need using `Addressable`

def url_for(options={})
val = super

if params[:locale].present?
parsed = Addressable::URI.parse(val)
query_array = parsed.query_values(Array) || []
query_array << ['locale', params[:locale]]
parsed.query_values = query_array
val = parsed.to_s
end

val
end
Reply

#3
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
Reply

#4
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` returns `Nil` if the actual url doesn't have existing query values.

Therefore if you have urls such as: `http://www.foo.com` and `http://www.foo.com?bar=1` you should use the following code:

url = "http://www.foo.com" # or params[:callback] obviously. :)

callback = Addressable::URI.parse(url)
callback.query_values = (callback.query_values || {}).merge({
result: true
})

redirect_to callback.to_s

Cheers.
Reply

#5
I think you're pretty close to optimal. you could crush out a line or two,
but it doesn't really gain you anything.

callback = Addressable::URI.parse(params[:callback])
callback.query_values = callback.query_values.merge {:results => 'true' }
redirect_to callback.to_s


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.
Reply

#6
callback.query_values = callback.query_values.merge({"result" => "true"})
Reply

#7
You can try with `merge`

`request.parameters.merge({:result => true})`

this will add your parameter to the ones already defined.
Reply

#8
- if you don't need any URL validations you can do this (looks a little bit dirty):<pre><code>
url = params[:callback]
redirect_to url + (url.include?('?') ? '&' : '?') + 'result=true'
</code></pre>
- otherwise you have to use either some external library (like Addressable) or URI module from standard library
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through