Rails:跟踪用户 ID

发布于 2024-07-15 03:26:01 字数 167 浏览 6 评论 0原文

在我的 Rails 应用程序中,我有一个登录页面。 该人登录后,我的应用程序继续跟踪已登录人员的最佳方式是什么。例如,如果用户移动到不同的页面,我的控制器/操作将失去对该用户的跟踪,除非我继续传递用户随后访问的每个页面之间的变量。 有更好的方法吗? 我应该使用 sessions 变量吗?

In my Rails app, I have a login page. After that person logs in, what is the best way for my app to continue tracking the person that has logged in. For example, if the user moves to different pages, my controllers/actions will lose track of that user unless I keep passing a variable between each page the user subsequently visits. Is there a better way of doing this? Should I be using the sessions variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪我闹别瞎闹 2024-07-22 03:26:01

是的,会议正是您所寻找的。

session["user_id"] = user_id

并在另一个页面上获取当前用户(如果您的模型称为 User):

@current_user = User.find(session["user_id]")

Yes, sessions are exactly what you are looking for.

session["user_id"] = user_id

And to fetch the current user on another page (if your model is called User):

@current_user = User.find(session["user_id]")
错爱 2024-07-22 03:26:01

强烈考虑使用插件来管理它。

有几种,例如静态身份验证

这提供了 current_userlogged_in? 功能。

Strongly consider a plugin to manage this.

There are several, such as restful authentication.

This gives current_user and logged_in? functionality.

凉城 2024-07-22 03:26:01

有一些很棒的宝石可以做到这一点

There are some great gems that do this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文