Using redirect_to and passing additional parameters

I am trying to return redirect_to and pass additional parameters. This is what I have in the controller:

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

      

This redirects to the url correctly, but no hello is sent. Ideas?

+3


source to share


4 answers


Is it a env['omniauth.origin']

string? If so, I don't think it can work. You can try adding a parameter like:

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

      



or something like that.

+5


source


redirect_to

eventually calls url_for

, and if the argument url_for

is a string, it just returns that string unchanged. He ignores any other options.

I would suggest just using the Rails method Hash#to_query

:



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

      

+4


source


Add the path to it in your routes and pass helloworld as a parameter

redirect_to(route_in_file_path('helloworld'))

      

+1


source


Add function to ApplicationController

class

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

      

Now you can do the following:

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

      

0


source







All Articles