简体   繁体   English

成功进入DEVISE后,如何将用户重定向回以前需要登录的操作?

[英]After a successful DEVISE login, how to redirect user back to previous action that required login?

I have an ajax voting button: If a user clicks on a "thumbs up" image, but is not logged in, then they should see a dialog box that asks them to login first. 我有一个ajax投票按钮:如果用户点击“竖起大拇指”图像,但没有登录,那么他们应该会看到一个对话框,要求他们先登录。

To do this dialog box,I'm using jQuery and facebox bound to ajax:failure event. 要执行此对话框,我使用jQuery和facebox绑定到ajax:failure事件。 Devise will raise a 401 Unauthorized if the user is not logged in. facebox loads the remote html for the login in a pop-up dialog box that presents the DEVISE signin form. 如果用户未登录,Devise将引发401 Unauthorized .facebox在弹出对话框中加载远程html以进行登录,该对话框显示DEVISE登录表单。

Everything works except after successful login, user is redirected to the homepage. 一切正常,除了成功登录后,用户被重定向到主页。 It would be more intuitive if user was redirected back to the post they were looking at so they can continue their vote. 如果用户被重定向回他们正在查看的帖子,那么他们可以继续投票,这将更加直观。

Is there a best practices way of achieving this behavior? 是否有实现此行为的最佳实践方法?

Thanks. 谢谢。

I do the following with Authlogic on Rails 3, perhaps you could use it or do something similar: 我在Rails 3上使用Authlogic执行以下操作,也许您可​​以使用它或执行类似的操作:

class ApplicationController < ActionController::Base
  after_filter :store_location
private
  def store_location
    session[:return_to] = request.fullpath
  end

  def clear_stored_location
    session[:return_to] = nil
  end

  def redirect_back_or_to(alternate)
    redirect_to(session[:return_to] || alternate)
    clear_stored_location
  end
end

Then you can use something like redirect_back_or_to dashboard_path in your login action. 然后,您可以在登录操作中使用redirect_back_or_to dashboard_path

For some controllers/actions, I don't want to store the location (eg the login page). 对于某些控制器/操作,我不想存储位置(例如登录页面)。 For those I just skip the filter: 对于那些我只是跳过过滤器:

class UserController < ApplicationController
  skip_after_filter :store_location
end

There's also a page in the Devise wiki on doing this: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in 在Devise wiki中还有一个关于这样做的页面: https//github.com/plataformatec/devise/wiki/How-To :-Redirect-to-a-specific-page-on-successful-sign-in

In ApplicationController write following: 在ApplicationController中写如下:

after_filter :store_location
def store_location
  session[:previous_urls] ||= []
  # store unique urls only
  session[:previous_urls].prepend request.fullpath if session[:previous_urls].first != request.fullpath
  # For Rails < 3.2
  # session[:previous_urls].unshift request.fullpath if session[:previous_urls].first != request.fullpath 
  session[:previous_urls].pop if session[:previous_urls].count > 2
end
def after_sign_in_path_for(resource)
  session[:previous_urls].last || root_path
end

Source: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update 资料来源: https//github.com/plataformatec/devise/wiki/How-To : -Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update

use a cookie. 使用cookie。 Get the window.location.href that they are trying to go to and store it as a cookie. 获取他们尝试去的window.location.href并将其存储为cookie。 On your home page, check for the presence of this cookier in the document ready function, redirect if its there 在您的主页上,检查文档就绪功能中是否存在此cookier,如果存在则重定向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 和 Devise:成功登录后如何将用户重定向回请求的页面? - Rails and Devise: How to redirect user back to the requested Page after successful login? 通过devise注册后如何将用户重定向回登录表单? - How to redirect user back to login form after signing up with devise? 如何在 devise 令牌验证中确认 email 后重定向到登录页面/或使用户登录成功 - How to redirect to login page/ or making user login successful after confirm email in devise token auth 设计:成功登录后重定向到管理路径 - Devise: Redirect to Admin Path After Successful Login 使用Devise登录后重定向到上一页 - Redirect to previous page after login with Devise 设计:登录尝试失败后重定向回 - Devise: redirect back after unsuccessful login attempt 用户请求新的确认后如何重定向到设计登录页面 - How to redirect to the Devise login page after user requests new confirmation 设计:成功登录后勾选 - Devise: after successful login hook Devise:登录后如何重定向到上一页,但前提是前一页需要身份验证? - Devise: How to redirect to the previous page after login, but only if previous page requires authentication? Rails / Devise /链接到登录页面,并向其传递重定向路由,以在成功登录后使用 - Rails / Devise / Link to login page and pass it a redirect route to use after successful login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM