mercredi 6 janvier 2016

ActionView::Template::Error: undefined method `name' for nil:NilClass

I do not manage to understand the following error that pops up when I run my tests

 ActionView::Template::Error:
   undefined method `name' for nil:NilClass
 # ./app/views/shared/_navbaruser.html.erb:5:in `_app_views_shared__navbaruser_html_erb__1173572034888802020_70230060606960'
 # ./app/views/confirmations/indexuser.html.erb:9:in `_app_views_confirmations_indexuser_html_erb___3677980567853899736_70230060569840'
# ./spec/views/confirmations/indexuser.html.erb_spec.rb:5:in `block (2 levels) in <top (required)>'

My template indexuser calls a partial _navbaruser with the following line

<%= render "shared/navbaruser" %>

The partial has

<%= @current_user.name %>

Current_user is defined in my helper

def current_user
if (user_id = session[:user_id])
  @current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
  user = User.find_by(id: user_id)

   if user && user.authenticated?(:remember, cookies[:remember_token])
  log_in user
    @current_user = user
  end
  end
end

I get the error when I run my test. The application works fine and I do get the name of the current_user when someone is logged in. In the controller I tried:

1) To fully empty the controller (I do not put anything under the method that calls the template)

2) Just put one line to define current_user

@current_user = User.first

In both cases, I get the error when I run my test. Could anyone give me some hints on where to look for to correct this error? Could it be that defining current_user in a helper method triggers this template error issue? Thank you.

Aucun commentaire:

Enregistrer un commentaire