Append query string to url

后端 未结 6 1547
[愿得一人]
[愿得一人] 2021-02-05 08:15

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 o

6条回答
  •  臣服心动
    2021-02-05 08:33

    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.

提交回复
热议问题