简体   繁体   中英

Signing in a User (Ruby on Rails Tutorial Ch. 8)

Why is the middle method necessary? It seems to me like it's just an intermediary step to connect the first and third methods.

module SessionsHelper

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

  def current_user=(user)
    @current_user = user
  end

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

It is the setter method or the helper method to set the current_user with the user who is currently online . This is used by devise gem to identify the current user.

So whenever you need to find the online user for your application, you just use the following code-

if current_user
#Do something important   
else
#You do not have enough privileges. Please login.
#Your offline stuff
end 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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