开发者

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[:refere开发者_如何学Pythonr])
    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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜