设计和存储_位置:您如何存储返回位置? [英] Devise and stored_location_for: how do you store the return location?

查看:112
本文介绍了设计和存储_位置:您如何存储返回位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户没有登录,我有一个页面,其路径是(例如)/处所/ 92,我正在显示请[登录]或[注册]以获取更多信息在用户登录后返回相同/前景/ 92页。

I have a page whose path is (e.g.) /premises/92 on which I'm displaying "please [log in] or [register] for additional information" if the user is not logged in, and I want devise to return that same /premises/92 page after the user logs in.

我已经阅读过其他帖子,我想我了解devise的 stored_location_for 应该工作。理论上,我可以把这样的东西放在我的 ApplicationController 中:

I've read other posts and I think I understand how devise's stored_location_for is supposed to work. In theory, I could put something like this in my ApplicationController:

def stored_location_for(resource)
  if (r = session[:return_to])
    session[:return_to] = nil
    r
  else
    super
  end
end

我的问题是:如何/我在哪里设置会话[:return_to]?

My question is: how / where do I set up session[:return_to]?

只有当用户点击[登录]或[注册]时,我想设置会话[:return_to],但最好的方法是什么?

I want to set session[:return_to] only if the user clicks on [log in] or [register], but what's the best way to do that?


  • 使用JavaScript装饰链接?这可以工作,但似乎很重。

  • 在渲染页面之前将其设置在Premises Controller中?那似乎不对,如果用户没有点击[登录]或[注册]链接怎么办?然后我将会话[:return_to]设置为一些奇怪的值,如果用户从其他页面登录,可能会让我失望。

  • 向[登录]和[注册]链接添加?return_to = / premises / 92 查询字符串,并检测在RegistrationsController和SessionsController中,并使用该信息设置会话[:return_to]?这似乎是有效的,但也是沉重的。

  • Decorate the links with JavaScript? That could work, but seems heavy-handed.
  • Set it in the Premises Controller before rendering the page? That doesn't seem right: what if the user doesn't click on the [log in] or [register] links? Then I have session[:return_to] set to some odd value which might trip me up if the user logs in from some other page.
  • Add a ?return_to=/premises/92 query string to the [log in] and [register] links, and detect that in the RegistrationsController and SessionsController and use that info to set up session[:return_to]? That seems like it would work, but also heavy-handed.

这些气味都没有。

推荐答案

设计使用

session["#{scope}_return_to"]

所以你可以使用 session [user_return_to] ,如果你的验证模型是User。

So you can use session["user_return_to"] if your model for authentication is User.

这篇关于设计和存储_位置:您如何存储返回位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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