开发者

Rails 3 helper_method

开发者 https://www.devze.com 2023-03-24 13:54 出处:网络
I have setup a helper method within the application controller ie. class ApplicationController < ActionController::Base

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_开发者_C百科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

#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 ?

Thanks for the help


It's a method, you're trying to access an instance variable, do it like this:

Welcome <%= current_user.first_name%>
0

精彩评论

暂无评论...
验证码 换一张
取 消