AnApprentice
AnApprentice

Reputation: 110950

Using redirect_to and passing extra params

I'm trying to return redirect_to and pass extra params. Here is what I have in my controller:

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

This is redirecting to the URL correctly but hello is not being passed. Ideas?

Upvotes: 3

Views: 2695

Answers (4)

Harish Shetty
Harish Shetty

Reputation: 64363

Add a 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")

Upvotes: 0

Brandan
Brandan

Reputation: 14983

redirect_to eventually calls url_for, and if the argument to url_for is a String, it simply returns that String untouched. It ignores any other options.

I'd suggest simply using Rails's Hash#to_query method:

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

Upvotes: 4

Sairam
Sairam

Reputation: 2898

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

redirect_to(route_in_file_path('helloworld'))

Upvotes: 1

tsherif
tsherif

Reputation: 11710

Is env['omniauth.origin'] a String? If so, I don't think this can work. You could try adding the parameter as:

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

or something to that effect.

Upvotes: 5

Related Questions