关于Ruby on Rails上的helper方法

时间:2018-06-16 12:41:02

标签: ruby-on-rails ruby

我试图从Ruby on Rails Tutorial学习Ruby on Rails。 但是我在第7章列表8.29中遇到了一个问题,为什么我可以在@current_user app/helpers/sessionse_helper方法中使用log_out

module SessionHelper

  def log_in(user)
    session[:user_id] = user.id
  end

  def current_user
    @current_user ||= User.find_by(id: session[:user_id])
  end

  def logged_in?
    !current_user.nil?
  end

  def log_out
    session.delete(:user_id)
    @current_user = nil
  end
end

如果有人知道这个功能,请告诉我。

1 个答案:

答案 0 :(得分:0)

@current_user used to set the logged in user in the application. Once you are logged out from the application, log_out method setting it to nil so that it is not accessible anywhere within the application hence make it complete logout.