2012-04-05 153 views
3

我试图返回redirect_to并传递额外的参数。这里是我在我的控制器中有:使用redirect_to并传递额外参数

redirect_to(env['omniauth.origin'], :hello => "hello world") 

这是重定向到URL正确,但你没有被传递。想法?

+0

是您的目标(omiauth)外部网址? – Deradon 2012-04-05 23:50:32

+0

不,它不是一个外部URL – AnApprentice 2012-04-05 23:55:11

回答

5

env['omniauth.origin'] a String?如果是这样,我认为这不可行。您可以尝试添加参数为:

redirect_to(env['omniauth.origin'] + "?hello=helloworld") 

或其他相关内容。

1

添加路径它在你的路线,并通过HelloWorld作为参数

redirect_to(route_in_file_path('helloworld')) 
4

redirect_to最后调用url_for,如果参数url_for是一个字符串,它只是返回字符串不变。它忽略了其他选项。

我建议干脆使用Rails的Hash#to_query方法:

redirect_to([env['omniauth.origin'], '?', params.to_query].join) 
+1

我应该注意,如果'env ['omniauth.origin']'还没有包含查询字符串,这将只会生成一个有效的URL。 – Brandan 2012-04-06 03:07:32

0

添加功能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") 
相关问题