Home>

I am doing a Rails tutorial (6th edition, Chapter 8, List 8.33).

Logged_in? And is_logged_in to determine if a user is logged in, for product and testing? I have defined a method, but logged_in? Is determined by the presence or absence of current_user, and is_logged_in? Is determined by the presence or absence of user_id of session.

I was wondering why the contents are different even though it is the same method to determine "I am logged in", so I asked a question.

Thanks for your reply.

sessons_helper.rb
  def log_in (user)
    session [: user_id] = user.id
  end
  def current_user
    if session [: user_id]
      @current_user || = User.find_by (id: session [: user_id])
    end
  end
  def logged_in?
    ! current_user.nil?
  end
test_helper.rb
  def is_logged_in?
    ! session [: user_id] .nil?
  end
  • Answer # 1

    Since it is another person's code written by another person, if you are trying to confirm the intention, only the person who made the code (or the person involved in the tutorial) can answer

Related articles