问题描述
我有一个页面,其路径为(例如)/premises/92,如果用户未登录,我将在该页面上显示请[登录] 或[注册] 以获取更多信息",并且我想设计用户登录后返回相同的/premises/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.
我读过其他帖子,我想我明白设计的 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]?
我只想在用户点击 [登录] 或 [注册] 时设置 session[: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 中设置它?这似乎不对:如果用户不点击 [登录] 或 [注册] 链接怎么办?然后我将 session[:return_to] 设置为一些奇怪的值,如果用户从其他页面登录,这可能会使我绊倒.
- 将
?return_to=/premises/92
查询字符串添加到 [log in] 和 [register] 链接,并在 RegistrationsController 和 SessionsController 中检测它并使用该信息来设置会话[:还给]?这似乎可行,但也很严厉.
- 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.
这些味道都不对.为stored_location_for 设置状态的普遍接受的技术是什么?
None of these smell right. What's the generally accepted technique for setting up state for stored_location_for?
推荐答案
设计使用
session["#{scope}_return_to"]
因此,如果您的身份验证模型是 User,您可以使用 session["user_return_to"]
.
So you can use session["user_return_to"]
if your model for authentication is User.
这篇关于Devise 和stored_location_for:如何存储返回位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!