redirect_to外部URL传递参数

Lea*_*xxx 3 controller ruby-on-rails-3

我需要将redirect_to方法中的params传递给外部URL ...

我知道我可以通过重定向做这样的事情:

redirect_to my_url_path(param1: "foo", param2: "bar")
Run Code Online (Sandbox Code Playgroud)

但我想用外部网址来做这件事.例如:

redirect_to "www.example.externaldomain.com/process/XIGHTDJTRIDEOR", param1: "foo", param2: "bar"
Run Code Online (Sandbox Code Playgroud)

pol*_*iro 17

您可以使用ruby URI模块并创建自己的帮助程序:

def generate_url(url, params = {})
  uri = URI(url)
  uri.query = params.to_query
  uri.to_s
end
Run Code Online (Sandbox Code Playgroud)

然后只需获取网址:

redirect_to generate_url("www.example.externaldomain.com/process/XIGHTDJTRIDEOR", :param1 => "foo", :param2 => "bar")
Run Code Online (Sandbox Code Playgroud)