登录或注册后,如何使Devise重定向到存储的位置? [英] How do I make Devise redirect to a stored location after sign in or sign up?
问题描述
我正在使用Devise在我写的Rails应用程序中,我想让用户在登录或注册后返回到他们的位置。
I'm using Devise in a Rails application I'm writing, and I want to let users go back to where they were after signing in or signing up.
例如,如果我有一个受到以下保护的评论控制器:
For example, if I have a "comments" Controller that is protected by:
before_filter :authenticate_user!
然后我想要点击立即评论的用户。按钮(并因此被重定向到CommentController中的新操作)登录,然后将Devise重定向到CommentController中的新操作(或任何位置),而不是应用程序的通用根,或通用的after_sign_in_path。
Then I want users who click a "Comment Now!" button (and are therefore redirected to the new action in CommentsController) to log in and then have Devise redirect them to the new action (or wherever they were) in CommentsController, not to the generic root of the application, or to a generic after_sign_in_path.
通过RDOC for Devise,我发现这种方法,使得它看起来好像Devise至少具有能力可以自己做这样的事情,但是我不能找出一个方法。
Looking through the RDOC for Devise, I found this method that makes it look as if Devise has at least the capability to do something like this on its own, but I can't figure out a way.
推荐答案
好的,所以我做了一些更多的实验,并与Kormie的信息一起工作,得到一个工作的解决方案。
OK, so I've done some more experimentation, and working with Kormie's info, I've got a working solution.
从我可以确定, before_filter authenticate_user!
不 >保存返回用户的路由。我所做的是这样的:
From what I can determine, before_filter authenticate_user!
does not save the route for returning the user. What I did was this:
首先,我在控制器顶部添加了一个额外的 before_filter
First, I added an extra before_filter
at the top of my controller
before_filter :store_location
before_filter :authenticate_user!
然后,我写了 store_location
控制器的底部
Then, I wrote the store_location
method at the bottom of the controller
private
def store_location
session[:user_return_to] = any_old_route_path
end
我不声称这是完美的,但它适用于我。 (其他任何人想要使用它的缺点就是它只支持一个控制器的返回路径,这就是我需要的所有东西,但是我以前使用的每个应用程序的返回路径只是稍微改进一点。 )我真的很感激任何人的见解和建议。
I don't claim this is perfect, but it works for me. (The downside for anyone else wanting to use it, is that it only supports one return path per controller. This is all I need for myself, but it is only a slight improvement over the one return path per app that I was using previously.) I would really appreciate anyone else's insights and suggestions.
这篇关于登录或注册后,如何使Devise重定向到存储的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!