简体   繁体   中英

Redirect after the sign up

Im trying to redirect the user after the sign up by saving the referer in case when user came to sign up through clicking on any specific page. But its not working properly.

In my app controller

def save_referer
  unless user_signed_in?
    unless session['referer']
      session['referer'] = request.referer || 'none' 
    end
  end
end

In user model

def save_with(referer)
  referer = referer unless referer == "null"
  self.save
end 

Here im saving it

if current_user.sign_in_count <= 1
  if current_user.save_with(session[:referer])
    redirect_to session[:referer]
  else
    redirect_to any_other
  end

In User model, I guess you are not explicitly referring the attribute correctly.

def save_with(referer)
  self.referer = referer unless referer == "null"
  self.save
end

Generally, I take this strategy:

Anywhere I have a signup button I link to signup like so:

<a href="/signup?signup_refer=/current_path">Signup!</a>

Then in my signup's Action I do this:

def signup
  session[:signup_refer] = params[:signup_refer] if params[:signup_refer] # put the refer in a session
  redirect_to(signup_path) if session[:signup_refer] && params[:signup_refer] # this clears params if you want to keep your url nice an clean for the user
  # do signup stuffs
  signup_refer = session[:signup_refer] # get the refer info in a variable
  session[:signup_refer] = nil # clear the session before redirecting
  redirect_to(signup_refer ? signup_refer : "/default_post_signup")
end

I know it could be messy, but it totally works for me (well works in Merb, where it's " redirect() not redirect_to() ) but you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM