kakubei
kakubei

Reputation: 5400

Rails 3 redirect to external page with parameters

I have this link:

railsapp/api/details.php?id=1&app=dvd 

That I need to redirecto to another page in an external non-rails server:

non-railsapp/api/details.php?id=1&app=dvd 

If I put this in the routes.rb file:

  match '/api/details.php' => redirect("non-railsapp/api/details.php")

The redirection will work, but the parameters will not be passed. What magic needs to be there so that the params are passed as well?

In Fact, what would be great would be to be able to redirect everything after the /api/ to the external site with all parameters. That would make my life a lot easier.

I've read the documentation on Rails Routing but cannot find any examples of something like this.

Thanks.

Upvotes: 2

Views: 960

Answers (2)

chrisandrew.cl
chrisandrew.cl

Reputation: 867

You can do the following:

url = URI("http://www.google.com/")

url.query = URI.encode_www_form(:q => "query")

#redirecting to http://www.google.com/?q=query

redirect_to url.to_s

Upvotes: 2

Daniel
Daniel

Reputation: 4173

Try something like this:

get "/api/details.php/:id/:app" => redirect{ |p, req| "non-railsapp/api/details.php/#{p[:id]}/#{p[:app]}" }

Edit:

You could also make the parameters optional:

get "/api/details.php(/:id(/:app))" => redirect{ |p, req| "non-railsapp/api/details.php(/#{p[:id]}(/#{p[:app]}))" }

Upvotes: 3

Related Questions