简体   繁体   English

Rails 3 helper_method

[英]Rails 3 helper_method

I have setup a helper method within the application controller ie. 我在应用程序控制器中设置了一个辅助方法,即。

class ApplicationController < ActionController::Base

  # Helpers
  helper_method :current_user

  # Private Methods
  private

  def current_user
    @current_user ||= Tester.find(session[:user_id]) if session[:user_id]
  end

end

If I try to access to current_user variable within a view im getting an error 如果我尝试访问视图中的current_user变量,则会出现错误

#code
Welcome <%=@current_user.first_name%>

#error
undefined method `first_name' for nil:NilClass

I know the session is good. 我知道会议很好。 Is this the correct was to access to current_user ? 这是正确的访问current_user?

Thanks for the help 谢谢您的帮助

这是一个方法,你试图访问一个实例变量,这样做:

Welcome <%= current_user.first_name%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM