我目前陷入了导轨教程https://www.railstutorial.org/book/log_in_log_out#sec-logging_in
的第8课我得到错误未定义的方法`logged_in?'这是我的代码
任何帮助将不胜感激
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
端
<!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
答案 0 :(得分:1)
?
中的方法名称中缺少SessionsHelper
。你想要这个:
def logged_in?
!current_user.nil?
end
您收到此错误:
undefined method `logged_in?'
因为目前您没有名为logged_in?
的方法,但您只有一个名为logged_in
的方法。因此,您只需将方法名称更改为logged_in?
即可。然后,它会找到方法并且可以正常工作。