Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the referer url of a previous page in Rails 4

I will try to put it in a simple way. I have a Sign Up button (which redirects to /users/sign_up page) on /visitors/owner-faq page. I want the URL of this page(/visitors/owner-faq) after signing up so that i can use it to set something like

if sign_up_from_url == '/visitors/owner-faq'
 @user.role = 'Owner'
end

I tried to use request.referer in the controller, but it gave me /users/sign_up not /visitors/owner-faq. However when I used <%= url_for(:back) %> in the sign up view page, it gave me /visitors/owner-faq which I want.But I cannot figure out how to access it in the controller. Can anyone suggest me how to access that value in controller? I'm using Devise for sign up.

like image 476
Pavan Avatar asked Jun 06 '15 16:06

Pavan


People also ask

How do I get the previous URL in Ruby on Rails?

How do I get the previous URL in Ruby on Rails? In a web application there is no such thing as a previous url. The http protocol is stateless, so each request is independent of each other. You could have the Javascript code, that sends a request back, send the current url with the request.

How do you find a reference URL?

There is a global variable in PHP that contains the referred URLs, called “$_SERVER.” The “$_SERVER” variable includes the “HTTP_REFERER” element that will return the referrer URLs. The combining “$_SERVER['HTTP_REFERER']” variable will display the complete URLs of the webpage that the current page is linked from.

How do I redirect back to the page in rails?

In Rails 4. x, for going back to previous page we use redirect_to :back. However sometimes we get ActionController::RedirectBackError exception when HTTP_REFERER is not present. This works well when HTTP_REFERER is present and it redirects to previous page.

What is request referer in rails?

request.referer gives you the previous URL or / if none. It is usually used to redirect the user back to the previous page (link)


2 Answers

What happens is if the user is on /visitors/owner-faq page and clicks on the sign up link, the referrer in the sign up page is /visitors/owner-faq. This is the reason why url_for(:back) works on that page. Now, after submitting the form in the sign up page, the referrer is now changed to the sign up url, which makes sense.

What you want to do is save the referrer in the session when the user visits the sign up page. So in the action that serves the sign up page (say that's the users new action)

class UsersController < ApplicationController
  def new
    session[:referrer] = request.referrer
  end
end

So when the user submits the sign up form, you can access the session

def create
  referrer  = session.delete(:referrer)
  user      = User.new
  user.role = 'owner' if referrer == '/visitors/owner-faq'
  user.save!
end
like image 81
jvnill Avatar answered Oct 21 '22 02:10

jvnill


Even you don't have to store in session

if request.referrer == sign_up_from_url
  super 
end

This will give you last referrer before sign_up_from_url.

like image 42
Vrushali Pawar Avatar answered Oct 21 '22 02:10

Vrushali Pawar