简体   繁体   English

在ruby中创建登录会话时了解“ current_user”概念

[英]Understand “current_user” concept when creating a login session in ruby

I am going through the great Michael Hartl tutorial to build ruby app here . 我正在阅读出色的Michael Hartl教程,以在此处构建ruby应用程序。

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

in this method: 在这种方法中:

module SessionsHelper

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

I understand the whole concept of creating a cookie with the user_token. 我了解使用user_token创建cookie的整个概念。

But I don't understand what does self.current_user = user means and 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? 但是我不明白 self.current_user = user 含义,为什么还要保留这行代码-我的cookie带有令牌-为什么我需要知道当前用户?

Also, where does this "self" is being stored - it is not like a flash[:success] parameter I can see in one of my views. 另外,这个“自我”存储在哪里-就像我在其中一个视图中看到的flash[:success]参数一样。 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 

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? 而且我仍在尝试为这个神秘的current user连接目的的点-它的目的是创建@current_user全局变量以在视图中使用吗?

If so - why there are there these 2 duplicated functions def current_user=(user) and def current_user 如果是这样-为什么会有这两个重复的函数def current_user=(user)def current_user

A few things. 一些东西。

First, you're reading the method names wrong (which is not surprising given how cryptic ruby method naming can be). 首先,您读错了方法名称(鉴于隐秘的ruby方法命名可能如此,这不足为奇)。 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. def current_user=(user)实际上被理解为定义了使用参数user current_user=方法,而def current_user定义了不使用参数的current_user方法。 These are referred to respectively as setters and getters . 这些分别称为settergetter

Here's a reference: Ruby (programming language): What are setters and getters in Ruby? 参考: Ruby(编程语言):Ruby中的setter和getter是什么?

So that explains the duplication. 这样就解释了重复。 On to your next question. 关于您的下一个问题。

I don't understand what does self.current_user = user means 我不明白self.current_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). self是一个关于自身的话题,值得自己讨论,所以我什至不会尝试解释它(这里是许多参考中一个 )。 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). 出于这个问题的目的,记住要设置实例变量,即使在类中也需要在自己的前缀上加上self ,这是非常重要的(出于其他目的,它是隐式的)。 The rest of the line is a call to the current_user= setter method I mentioned above, with the argument user . 该行的其余部分是使用参数user调用上面提到的current_user= setter方法。

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? 为什么甚至必须保留这行代码-我的cookie带有令牌-为什么我需要知道当前用户?

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: 看一下getter方法:

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

What this says is: if I haven't looked up and set the instance variable @current_user yet, then look it up; 这是说:如果我还没有查找并设置实例变量@current_user ,则查找它; if I have already set it, then just return it. 如果我已经设置好了,那就退货吧。 That saves a lot of looking up. 这样可以节省很多查找时间。

I think that answers your questions. 我认为这可以回答您的问题。 There are a lot of deeper issues ( self , etc.) which you can find more information about elsewhere. 有很多更深层次的问题( self等),您可以在其他地方找到更多信息。 Here's one discussion of why you need to include self in setters on SO: Why do Ruby setters need "self." 这是关于为何需要在SO的setter中包含self讨论: Ruby setter为什么需要“ self”。 qualification within the class? 班内资历?

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. 更新:一点点澄清,关于在类中使用self进行setter的最后一个链接实际上有点题外话,因为您是在模块中而不是直接从类中调用它。 In the context of a module, the self in self.current_user = user will become the class that the module is included inside of, eg User.current_user if it was called within the class User , etc. Again, another topic of discussion unto itself... 在模块的上下文中, self.current_user = userself将成为模块包含在其中的类,例如,如果在User类中调用了User.current_user ,等等。同样,另一个讨论主题...

The method def current_user=(user) is basically a setter that the sign_in method uses in order to set the current_user. def current_user=(user)方法本质上是sign_in方法用来设置current_user的设置器。

def current_user will return the @current_user or if it is not set it will find it in the Users table by the remember_token. def current_user将返回@current_user;如果未设置,则将通过Remember_token在Users表中找到它。 This basically allows you get the current_user at any point in time. 基本上,这使您可以在任何时间获取current_user。

self.current_user in the context of the sign_in method will refer to the calling class or module in this case. self.current_user在sign_in方法的上下文将是指主叫类或模块在这种情况下。 It will be calling current_user from the Session Helper module. 它将从Session Helper模块中调用current_user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM