Ruby on Rails - request.env['HTTP_REFERER'] returns nil

前端 未结 2 1927
灰色年华
灰色年华 2020-12-10 16:13

I am trying to save the http_referer so I can send the user back to that url after he has logged in. Right now I have a before_filter in my controllers that sends them to a

2条回答
  •  囚心锁ツ
    2020-12-10 16:56

    No matter what solution you come up with, you need to be able to handle the empty referer case, because the client has the choice whether or not to specify it. (It's off when you're in Safari's private browsing and Chrome's incognito mode for example)

    In Rails you can use redirect_to :back, which will use the HTTP_REFERER if it is set, or raise an error otherwise.

    When using redirect_to :back, if there is no referrer, RedirectBackError will be raised. You may specify some fallback behavior for this case by rescuing RedirectBackError.

提交回复
热议问题