【问题标题】:Redirect from a specific page从特定页面重定向
【发布时间】:2011-02-07 19:45:30
【问题描述】:

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

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 的登录页面。

我不希望他们在成功登录后返回此页面,那么如果他们恰好来自 session/new,我将如何将他们重定向到新/路径?

【问题讨论】:

    标签: ruby-on-rails redirect


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-06-12
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 2018-03-15
      • 2010-12-06
      • 2012-10-29
      • 2019-02-05
      相关资源
      最近更新 更多