DEVISE登录成功后,如何将用户重定向到以前需要登录的动作? [英] After a successful DEVISE login, how to redirect user back to previous action that required login?

查看:96
本文介绍了DEVISE登录成功后,如何将用户重定向到以前需要登录的动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ajax投票按钮:如果用户点击竖起大拇指图像,但没有登录,那么他们应该看到一个对话框,要求他们先登录。

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.

要做这个对话框,我使用jQuery和facebox绑定到ajax:failure事件。如果用户未登录,Devise将提起401未经授权。facebox在弹出的对话框中加载用于登录的远程html,该对话框显示DEVISE登录表单。

To do this dialog box,I'm using jQuery and facebox bound to ajax:failure event. 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.

除了成功登录之后,一切都有效,用户被重定向到主页。如果用户重定向到他们正在查看的帖子,这样他们就可以继续投票,这将会更加直观。

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?

谢谢。

推荐答案

我使用Authlogic on Rails 3,也许你可以使用它或做类似的事情:

I do the following with Authlogic on Rails 3, perhaps you could use it or do something similar:

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

然后你可以使用类似在登录操作中redirect_back_or_to dashboard_path

Then you can use something like redirect_back_or_to dashboard_path in your login action.

对于某些控制器/操作,我不想存储位置(例如登录页面)。对于那些我只是跳过过滤器:

For some controllers/actions, I don't want to store the location (e.g. the login page). For those I just skip the filter:

class UserController < ApplicationController
  skip_after_filter :store_location
end

Devise wiki中还有一个页面这样做: https:/ /github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in

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登录成功后,如何将用户重定向到以前需要登录的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆