我定义了一个helper类,如下所示
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
我在我的应用程序控制器中包含了Helper类
class ApplicationController < ActionController::Base protect_from_forgery include SessionsHelper end
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
但是我无法从用户#show view访问’current_user’变量.
<% if signed_in? %> <p> <b>Current User:</b> <%= current_user.name %> </p> <% end %>
它说:nil的未定义方法`name’:NilClass