简体   繁体   English

Ruby On Rails教程第8章混乱

[英]Ruby On Rails Tutorial Chapter 8 Confusion

everybody! 大家好! Recently, I am working on Michael Hartle's RoR tutorial. 最近,我正在研究Michael Hartle的RoR教程。 In chapter 8, I encounter one problem which has confused me for two days. 在第8章中,我遇到了一个困扰我两天的问题。 Here is the problem. 这是问题所在。 In section 8.2.3. 在第8.2.3节。

module SessionsHelper

  def sign_in(user)
    .
    .
    .
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user     # Useless! Don't use this line.
  end
end

And Michael writes: 迈克尔写道:

If we did this, we would effectively replicate the functionality of attr_accessor, which we saw in Section 4.4.5.5 The problem is that it utterly fails to solve our problem: with the code in Listing 8.21, the user's signin status would be forgotten: as soon as the user went to another page—poof!—the session would end and the user would be automatically signed out. 如果我们这样做,我们将有效地复制attr_accessor的功能,我们在4.4.5.5节中看到问题是它完全无法解决我们的问题:使用代码清单8.21中的代码,用户的登录状态将被遗忘:as当用户转到另一个页面时,会话将结束,用户将自动退出。 To avoid this problem, we can find the user corresponding to the remember token created by the code in Listing 8.19, as shown in Listing 8.22. 为了避免这个问题,我们可以找到与代码清单8.19中的代码创建的记忆标记相对应的用户,如代码清单8.22所示。

The Listing 8.22. 清单8.22。

module SessionsHelper
  .
  .
  .
  def current_user=(user)
    @current_user = user
  end

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

My questions are: Why the previous code would make the session log out automatically when user go to a new page? 我的问题是:为什么以前的代码会在用户转到新页面时自动注销会话? Why the second piece of code wouldn't? 为什么第二段代码不会? I think, as long as a user log in, the @current_user's value will always be "user" until he log out explicitly, right? 我想,只要用户登录,@ current_user的值将始终为“user”,直到他明确注销为止,对吧?

The previous code doesn't so much log the user out, as doesn't re-create the user on subsequent requests. 之前的代码不会将用户注销,因为不会在后续请求中重新创建用户。

State is not shared across requests, and has to be re-created with every request. 状态不会在请求之间共享,并且必须随每个请求重新创建。 @current_user is an instance variable, and keeps it's value for the duration of a single request. @current_user是一个实例变量,并在单个请求的持续时间内保持其值。

To get around the fact that state is not shared, with each request we need to reload necessary variables such as @current_user from something that is common across the session, in this case they're using the remember_token cookie. 为了解决状态未共享这一事实,我们需要从每个请求中重新加载必要的变量,例如@current_user ,这些变量在会话中很常见,在这种情况下,它们使用的是remember_token cookie。

The first snippet of code does not reload @current_user on each request, so will forget what value it held as soon as the user browses to another page after logging in, the second snippet attempts to load the current user via the remember_token cookie, so after this has been set when someone logs in, should remember the user until that cookie expires. 第一段代码不会在每个请求上重新加载@current_user ,因此一旦用户在登录后浏览到另一个页面就会忘记它保持的值,第二个片段尝试通过remember_token cookie加载当前用户,所以之后这是在有人登录时设置的,应该记住用户,直到该cookie过期。

What he is saying here is that when we use @current_user in the first example when not on a sign in page, we have not called @current_user = User.find(1). 他在这里说的是,当我们在第一个例子中使用@current_user而不是登录页面时,我们没有调用@current_user = User.find(1)。 We are relying on it already having been set. 我们依靠它已经确定了。 Since we are not explicitly setting @current_user the following: 由于我们没有明确设置@current_user以下内容:

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

Says if the @current_user is not set, then set it by finding the user using the id stored in the cookie. 如果未设置@current_user,则通过使用存储在cookie中的id查找用户来设置它。 That way when we navigate to a page where we do not explicitly set the @current_user it will have been populated. 这样,当我们导航到我们没有明确设置@current_user的页面时,它将被填充。

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

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