我如何设计重定向到一个存储位置符号后或注册? [英] How do I make Devise redirect to a stored location after sign in or sign up?

查看:172
本文介绍了我如何设计重定向到一个存储位置符号后或注册?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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!

然后,我想谁点击一个用户的评论吧!按钮(因此被重定向到CommentsController新的动作)登录,然后让设计他们重定向到CommentsController新的动作(或无论他们是),而不是应用程序的通用根,或到通用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设计来看,我发现<一个href=\"http://rdoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers#stored_location_for-instance_method\">this方法,使得它看起来的,如果设计有至少作为的能力的做对自己这样的事情,但我不能想出一个办法。

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.

推荐答案

OK,所以我做了一些更多的实验,并与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

private

  def store_location
    session[:user_return_to] = any_old_route_path
  end

我不声称这是完美的,但它为我工作。 (其他任何人想利用它的缺点是,它仅支持每个控制器的一个回路。这是我需要为我自己,但它只是在说我是用$ P每个应用之一返回路径略有好转$ pviously)。我真的AP preciate别人的见解和建议。

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.

这篇关于我如何设计重定向到一个存储位置符号后或注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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