2

How do I - in Rails - receive a get request from one website (url), then save the parameters, and generate a new url of another website, with other parameters (calculated/ from db), and redirect to it?

I have no problem with the steps in the middle (e.g. saving to db, receiving parameters). The main question is how do I receive a get request, AND send a get request in Rails.

I saw according to the following question: How make a HTTP request using Ruby on Rails? that I can use the Net::HTTP class. However it seems there that they focus on requesting from another website some data, while here we are receiving hits from some website and want to redirect to another one.

My question is: 1. How do I save as a variable the url that the person arrived from (there is a paramater I need from there)? 2. How do I redirect the user to the new url at the end of the steps (saving, parsing new url, etc)? 3. Where do I perform these two main actions? In the controller? That is my instinct, however, doesn't the view hold the relevant url? where is the relevant index url people are referring to?

Community
  • 1
  • 1
Lucy Weatherford
  • 5,452
  • 16
  • 50
  • 76

1 Answers1

5

Perhaps I am not understanding well the question, but, assuming that your static_pages#index is receiving the get request:

in your StaticPagesController:

def index
  incoming_url = request.referer # this catch the incoming url, including its params
  incoming_url_params = URI(incoming_url).query # this returns an string with the incoming url params
  new_url = do_something_with_the_url_and_its_params_internally(incoming_url, incoming_url_params)
  redirect_to new_url
end

So as an example somebody coming from http://www.wikipedia.org?lang=es, that will be the incoming_url and incoming_url_params would be "lang=es"

Hope that helps

Galen
  • 957
  • 5
  • 16