26

I'm using Rails 3.0.9, Ruby 1.9.2, Devise 1.3.4 and has a need in Devise session new view to access the request url parameters although I see this as a general Rails request handling question. Essentially, a request is made to a Devise authenticated resource which redirects the user to the login screen. In the login view, I need access to the request url, eg, this is the request url called in the beginning

http://mysite.com/article/5?type=blah

In the redirect login page, I need access to that URL, anyone know how I can do this?

Johnny Klassy
  • 1,650
  • 5
  • 16
  • 22

2 Answers2

45

You can use request.referer or request.env['HTTP_REFERER'] in your controller to get the referer url.

topek
  • 18,609
  • 3
  • 35
  • 43
  • Tried that, didn't work, it shows the login screen url. By that, I meant in the sessions controller. If I put the code in `articles` controller, it wouldn't get executed until after the login is authenticated. – Johnny Klassy Nov 09 '11 at 21:32
  • If I understand your requirements right, you want to redirect the user to the page, where he was before the login action. Why don't you simply save the redirect url in the session, when the user clicks login? – topek Nov 09 '11 at 21:39
  • The redirection to the login page is already handled properly, what I need is on the login page asking for the user creds, I need access to the original request url, hope that makes more sense. I should clarify that the original call comes from another server, not a browser. – Johnny Klassy Nov 09 '11 at 21:50
  • I think you just have to save the referer before you redirect the user to the login page. And when the user successfully logs in redirect him to the stored value. – topek Nov 09 '11 at 21:59
  • Yeah it didn't work but found a workaround specific to Devise, thanks for your effort though. – Johnny Klassy Nov 09 '11 at 23:31
  • Hey Johnny, can you please share with us the workaround that you found ? I have the same thing here .. thanks – simo May 27 '12 at 00:55
  • http://stackoverflow.com/questions/2165665/how-do-i-get-the-current-url-in-ruby-on-rails – manish nautiyal May 28 '13 at 06:43
3

I have just found out that WEBrick handles request.referrer incorrectly. But don't worry. Unicorn handles it right. I did't test that on other servers. You should check this with yours. I don't think that you use WEBrick as a production server.

dRwg
  • 91
  • 1
  • 5
  • Indeed WEBrick is not appropriate for a production server. I also found differences in how WEBrick and Puma handled `request.referrer` - definitely worth checking if `request.referrer` doesn't behave as expected using WEBrick. – zelanix Apr 24 '16 at 10:43