Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

#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

like image 337
Lee Avatar asked Jul 25 '26 19:07

Lee


1 Answers

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

Welcome <%= current_user.first_name%>
like image 95
Maurício Linhares Avatar answered Jul 28 '26 12:07

Maurício Linhares



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!