Ruby, How to add a param to an URL that you don't know if it has any other param already

前端 未结 3 1790
忘掉有多难
忘掉有多难 2021-02-07 10:54

I have to add a new param to an indeterminate URL, let\'s say param=value.

In case the actual URL has already params like this

http://url.co         


        
3条回答
  •  温柔的废话
    2021-02-07 11:25

    More elegant solution:

    url       = 'http://example.com?exiting=0'
    params    = {new_param: 1}
    uri       = URI.parse url
    uri.query = URI.encode_www_form URI.decode_www_form(uri.query || '').concat(params.to_a)
    uri.to_s #=> http://example.com?exiting=0&new_param=1
    

提交回复
热议问题