gpt4 book ai didi

ruby-on-rails - 未定义的局部变量或方法 `sign_out'

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:17 25 4
gpt4 key购买 nike

当我点击注销链接时出现以下错误,即使该方法似乎存在。

NameError in SessionsController#destroy 
undefined local variable or method `sign_out' for #<SessionsController:0x007f87c8ecd8d8>

这是我的代码, session Controller

class SessionsController < ApplicationController

def new
end

def create
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# Sign the user in and redirect to the user's show page.
else
flash.now[:error] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end

def destroy
sign_out
redirect_to root_url
end
end

session 助手

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

def signed_in?
!current_user.nil?
end

def sign_out
self.current_user = nil
cookies.delete(:remember_token)end
end

布局

<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>

最佳答案

在 session Controller 中包含 session 助手。

class SessionsController < ApplicationController
include SessionsHelper
...
end

如果你想让 session 辅助方法在所有 Controller 中可用,请将其添加到应用程序 Controller 中而不是 session Controller 中。

关于ruby-on-rails - 未定义的局部变量或方法 `sign_out',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14636510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com