Reputation: 27
I am currently stuck at lesson 8 of the rails tutorial https://www.railstutorial.org/book/log_in_log_out#sec-logging_in
I get the error undefined method `logged_in?' Here is my code
Any help would be appreciated
module SessionsHelper
def log_in(user)
session[:user_id] = user.id
end
def current_user
@current_user = @current_user ||= User.find_by(id: session[:user_id])
end
# returns true if the user is logged in
def logged_in
!current_user.nil?
end
end
<!DOCTYPE html>
<html>
<head>
<%= stylesheet_link_tag 'application', media: 'all', 'data-
turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track'
=> true %>
<%= csrf_meta_tags %>
</head>
<body>
<header class = "navbar navbar-fixed-top navbar-inverse">
<div class = "container">
<%= link_to "sample app", home_path, id: "logo" %>
<ul class = "nav navbar-nav navbar-right">
<li> <%= link_to "Home", home_path %> </li>
<li> <%= link_to "About", about_path %> </li>
<% if logged_in? %>
<li> <%= link_to "Log Out", '#' %> </li>
<% end %>
</ul>
</div>
</header>
</div>
<div class = "container">
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
Upvotes: 2
Views: 131
Reputation: 34338
You are missing ?
in your method name in SessionsHelper
. You wanted to this:
def logged_in?
!current_user.nil?
end
You are getting this error:
undefined method `logged_in?'
because currently you don't have a method named logged_in?
, but you only have a method named logged_in
. So, you just have to change the method name to logged_in?
. Then, it will find the method and will work fine.
Upvotes: 1