我有一个 ajax 投票按钮:如果用户单击“竖起大拇指”图像,但尚未登录,那么他们应该看到一个对话框,要求他们先登录。
为了实现这个对话框,我使用 jQuery 和 Facebox 绑定到 ajax:failure 事件。如果用户未登录,Devise 将引发 401 未经授权。facebox 在显示 DEVISE 登录表单的弹出对话框中加载用于登录的远程 html。
一切正常,除了成功登录后,用户被重定向到主页。如果用户被重定向回他们正在查看的帖子,以便他们可以继续投票,那会更直观。
是否有实现这种行为的最佳实践方法?
Thanks.
我使用 Authlogic on Rails 3 执行以下操作,也许您可以使用它或执行类似的操作:
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
在您的登录操作中。
对于某些控制器/操作,我不想存储位置(例如登录页面)。对于那些我只是跳过过滤器:
class UserController < ApplicationController
skip_after_filter :store_location
end
Devise wiki 中还有一个关于执行此操作的页面:https://github.com/plataformatec/devise/wiki/How-To:-成功登录时重定向到特定页面 https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)