从特定页面重定向

时间:2011-02-07 19:45:31

标签: ruby-on-rails redirect

我正在尝试将用户重定向到他们登录的最后一页(使用模式) - 这是有效的。

if user  
    # Protects against session fixation attacks, causes request forgery
    # protection if user resubmits an earlier form using back
    # button. Uncomment if you understand the tradeoffs.
    # reset_session
    self.current_user = user
    new_cookie_flag = (params[:remember_me] == "1")
    handle_remember_cookie! new_cookie_flag
    format.html {
      if @invitation.try(:invitee_email) == user.email
        redirect_to(edit_invitation_path(@invitation))
      else
        begin
        # loop check
        if session[:last_back] != request.env['HTTP_REFERER']
        redirect_to(:back)
        session[:last_back] = request.env['HTTP_REFERER']
      else
        # raise on error
        raise ActionController::RedirectBackError
      end
        rescue ActionController::RedirectBackError
        # fallback on loop or other :back error
      redirect_to(:action => :index)
    end
 end

如果用户登录失败,他们会被重定向到会话/新的登录页面。

我不希望他们在成功登录后返回此页面,如果他们碰巧来自session / new,我将如何将它们重定向到新的/ path?

1 个答案:

答案 0 :(得分:1)

我认为有两种方法可以解决这个问题:

  1. 使用Ajax请求发布数据,而不是重定向登录,如果登录失败,则不会重定向。如果登录成功,请使用javascript重定向。
  2. 在应用程序控制器中使用before_filter,当用户注销时,会将当前页面作为重定向页面存储在会话中。在您不想记住的页面上跳过此过滤器,例如会话/新页面。用户登录后,检查会话以查找重定向页面并重定向到该页面(如果存在)。这样,无论用户访问会话/新的次数,他们都不会在登录后重定向到那里。 (您也应该对注销页面执行相同的操作 - 跳过前面的过滤器。)
相关问题