理解“current_user"在 ruby​​ 中创建登录会话时的概念 [英] Understand "current_user" concept when creating a login session in ruby

查看:38
本文介绍了理解“current_user"在 ruby​​ 中创建登录会话时的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读伟大的 Michael Hartl 教程来构建 ruby​​ 应用程序 这里.

I am going through the great Michael Hartl tutorial to build ruby app here.

我试图理解如何创建会话的概念,但我一直在理解这一行:

I am trying to understand the concept of how to create a session and I am stuck in understanding this line:

self.current_user = user

在这种方法中:

module SessionsHelper

  def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
  end
end

我了解使用 user_token 创建 cookie 的整个概念.

I understand the whole concept of creating a cookie with the user_token.

但我不明白 self.current_user = user 是什么意思以及为什么有必要保留这行代码 - 我有带有令牌的 cookie - 为什么我需要知道当前用户?

此外,这个自我"存储在哪里 - 它不像我可以在我的一个视图中看到的 flash[:success] 参数.所以我不明白它在哪里.

Also, where does this "self" is being stored - it is not like a flash[:success] parameter I can see in one of my views. so I don't understand where it is.

在同一个模块中也有这两个方法:

there are also these 2 methods in the same module:

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end 

而且我仍然试图将这个神秘的current user 的目的联系起来 - 它的目的是创建 @current_user 全局变量以在视图中使用?

And still I am trying to connect the dots of the purpose for this mysterious current user - is its purpose is to create @current_user global variable to use in the views?

如果是这样 - 为什么有这两个重复的函数 def current_user=(user)def current_user

If so - why there are there these 2 duplicated functions def current_user=(user) and def current_user

推荐答案

一些事情.

首先,您读错了方法名称(考虑到 ruby​​ 方法命名的神秘性,这并不奇怪).def current_user=(user) 实际上读作定义方法 current_user= 接受参数 user,而 def current_user 定义了一个不带参数的方法 current_user.它们分别称为settergetter.

First, you're reading the method names wrong (which is not surprising given how cryptic ruby method naming can be). def current_user=(user) is actually read as defining the method current_user= that takes an argument user, whereas def current_user defines a method current_user that takes no arguments. These are referred to respectively as setters and getters.

这是一个参考:Ruby(编程语言):Ruby 中的 setter 和 getter 是什么?

这就解释了重复.继续你的下一个问题.

So that explains the duplication. On to your next question.

我不明白 self.current_user = user 是什么意思

I don't understand what does self.current_user = user means

self 本身就是一个话题,值得自己讨论,所以我什至不会试图解释它(这里是 其中一个参考).就这个问题而言,重要的是要记住,为了设置实例变量,您需要在赋值前加上 self 前缀,即使是在类中(对于其他目的,它是隐式的).该行的其余部分是对我上面提到的 current_user= setter 方法的调用,使用参数 user.

self is a topic unto itself, worthy of its own discussion, so I won't even try to explain it (here's one reference out of many). For the purposes of this question it's just important to remember that in order to set instance variables, you need to prefix your assignment with self, even within the class (where for other purposes it would be implicit). The rest of the line is a call to the current_user= setter method I mentioned above, with the argument user.

为什么甚至需要保留这行代码 - 我有带有令牌的 cookie - 为什么我需要知道当前用户?

why is it even necessary to keep this line of code - I have the cookie with the token - why do I need to know the current user?

之所以有必要,是因为您不希望每次需要获取当前用户时都从令牌中查找用户.看一下getter方法:

The reason it's necessary is that you don't want to be looking up the user from the token every time you need to get the current user. Take a look at the getter method:

def current_user
  @current_user ||= User.find_by_remember_token(cookies[:remember_token])
end

这是什么意思:如果我还没有查找并设置实例变量@current_user,那么请查找它;如果我已经设置了它,那么只需返回它.这样可以节省大量查找时间.

What this says is: if I haven't looked up and set the instance variable @current_user yet, then look it up; if I have already set it, then just return it. That saves a lot of looking up.

我认为这可以回答您的问题.有很多更深层次的问题(self 等),您可以在别处找到更多信息.以下是关于为什么需要在 SO 的 setter 中包含 self 的讨论:为什么 Ruby 设置者需要自我".班级资格?

I think that answers your questions. There are a lot of deeper issues (self, etc.) which you can find more information about elsewhere. Here's one discussion of why you need to include self in setters on SO: Why do Ruby setters need "self." qualification within the class?

更新:小的澄清,关于在类中使用 self 的最后一个链接实际上有点偏离主题,因为您是在模块中调用它而不是直接从类中调用它.在模块的上下文中,self.current_user = user 中的 self 将成为模块包含在其中的类,例如User.current_user 如果它是在 User 类中调用的,等等.再次,另一个讨论主题......

UPDATE: Small clarification, that last link about using self for setters within the class is actually a bit off-topic, since you're calling it within a module and not directly from a class. In the context of a module, the self in self.current_user = user will become the class that the module is included inside of, e.g. User.current_user if it was called within the class User, etc. Again, another topic of discussion unto itself...

这篇关于理解“current_user"在 ruby​​ 中创建登录会话时的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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