Reputation: 33542
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.
Upvotes: 13
Views: 21437
Reputation: 3803
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
.
Upvotes: 2
Reputation: 29599
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
Upvotes: 25