我正在尝试返回 redirect_to 并传递额外参数。这是我在控制器中的代码:
这个重定向URL是正确的,但是"hello"没有被传递。有什么想法吗?
redirect_to(env['omniauth.origin'], :hello => "hello world")
这个重定向URL是正确的,但是"hello"没有被传递。有什么想法吗?
redirect_to(env['omniauth.origin'], :hello => "hello world")
env['omniauth.origin']
是一个字符串吗?如果是的话,我认为这种方法行不通。您可以尝试按以下方式添加参数:
redirect_to(env['omniauth.origin'] + "?hello=helloworld")
redirect_to
最终会调用 url_for
,如果传递给 url_for
的参数是字符串,则直接返回该字符串。它会忽略其他任何选项。
我建议使用 Rails 的 Hash#to_query
方法:
redirect_to([env['omniauth.origin'], '?', params.to_query].join)
env['omniauth.origin']
不包含查询字符串时,它才会生成有效的URL。 - Brandanredirect_to(route_in_file_path('helloworld'))
ApplicationController
类中添加一个函数。class ApplicationController < ActionController::Base
def update_uri(url, opt={})
URI(url).tap do |u|
u.query = [u.query, opt.map{ |k, v| "#{k}=#{URI.encode(v)}" }].
compact.join("&")
end
end
helper_method :update_uri # expose the controller method as helper
end
现在你可以做以下事情:
redirect_to update_uri(env['omniauth.origin'], :hello => "Hello World")