我正在尝试弄清楚如何使用Warden/Devise将用户重定向到他们登录(或登录失败)的页面。我认为在某个地方有一个会话变量,它要么是可用的,要么是可以使用的。
场景1:未授权用户进入受保护页面X;重定向到登录页面;用户登录;用户重定向到受保护页面X
场景2:未经授权的用户想要在页面x上执行受保护的操作;用户单击登录链接;用户登录;用户重定向到页面x,该操作现在可用
感谢任何人的指点。
谢谢!
发布于 2010-10-13 15:17:56
这里有一个名为after_sign_in_path_for(resource) (http://rdoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers)的巧妙帮助器方法,以及一个名为session[:"user.return_to"]的会话变量,用于存储最后的url。after_sign_in_path_for方法需要返回一个字符串,然后devise在登录后自动使用此路径重定向用户。
在我的应用程序控制器中,我放入了以下内容,如果没有设置会话变量,它会将我的用户重定向到主页:
def after_sign_in_path_for(resource)
(session[:"user.return_to"].nil?) ? "/" : session[:"user.return_to"].to_s
end发布于 2010-09-19 22:21:38
您可以使用request.referer获取以前的URL。
发布于 2010-12-04 01:50:52
如果您使用CanCan进行授权,则可以通过添加以下内容来完成此操作。如果没有,您应该能够将这些概念应用到您当前的授权系统中。
应用程序/控制器/应用程序_控制器.rb
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = exception.message
if user_signed_in?
redirect_to root_url
else
# Adds the protected page to the login url but only if the user is not logged in
redirect_to login_path(:next => request.path)
end
end
def after_sign_in_path_for(resource_or_scope)
# if a protected page found, then override the devise after login path
params[:user]["next"] || super
end应用程序/视图/设备/会话/new.html.erb
<% if params[:next] %>
<%= f.hidden_field :next, :value => params[:next] %>
<% end %>此解决方案不使用会话变量,而是使用URL中的params来跟踪受保护的页面。
https://stackoverflow.com/questions/3489064
复制相似问题