gpt4 book ai didi

ruby-on-rails - 从 View 中无法访问帮助程序类

转载 作者:行者123 更新时间:2023-12-04 03:36:12 24 4
gpt4 key购买 nike

我定义了一个帮助类如下

module SessionsHelper

def current_user
@current_user= User.find_by_fbid(session[:fbid])
end


def sign_in(user)
session[:fbid] = user.fbid
@current_user = user

end


def signed_in?
!current_user.nil?
end


end

我在我的应用程序 Controller 中包含了 Helper 类
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper


end

从 session Controller 调用登录方法
class SessionsController < ApplicationController


def create

user = User.find_or_create_by_fbid(params[:user][:fbid])
user.update_attributes(params[:user])
sign_in(user)
redirect_to user_path(user)

end

end

但是我无法从 users#show View 访问“current_user”变量。
 <% if signed_in? %>
<p>
<b>Current User:</b>
<%= current_user.name %>
</p>


<% end %>

它说:nil:NilClass 的未定义方法“名称”

有人可以请教吗?
方法 current_user 根本不会从索引中调用。

最佳答案

include SessionsHelper在您的 Controller 中包含 Controller 中的那些模块方法,因此它们可以在您的 Controller 方法中访问。您希望 View 中可用的辅助方法,因此您需要使用 helper SessionsHelper在您的应用程序 Controller 中。

话虽如此,我确实同意 Jits 的观点,即您在 SessionsHelper 中拥有的方法确实属于 Controller 而不是助手。

关于ruby-on-rails - 从 View 中无法访问帮助程序类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6239642/

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