Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Shaun Jackson
1,270 PointsUndefined method for nil:NilClass
I thought everything was going so well until...
Yep see, another bug already?
undefined method `full_name' for nil:NilClass
17: <li class="active"><%= link_to "Log Out", destroy_user_session_path %></li>
18: </ul>
19: <ul class="nav pull-right">
20: <li><%= link_to current_user.full_name, "#" %></li>
21: </ul>
22: </div>
23: </div>
Here is the full code from application layout:
<!DOCTYPE html>
<html>
<head>
<title>Treebook</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="#">Faceown</a>
<ul class="nav">
<li class="active"><%= link_to "New Ownage", new_status_path %></li>
<li class="active"><%= link_to "All Statuses", statuses_path %></li>
<li class="active"><%= link_to "Sign Up", new_user_registration_path %></li>
<li class="active"><%= link_to "Log Out", destroy_user_session_path %></li>
</ul>
<ul class="nav pull-right">
<li><%= link_to current_user.full_name, "#" %></li>
</ul>
</div>
</div>
<div class="container">
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</div>
</body>
</html>
I've checked the code and below is the code shown for full_name def.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation,
:remember_me, :first_name, :last_name, :profile_name
# attr_accessible :title, :body
has_many :statuses
def full_name
first_name + " " + last_name
end
end
2 Answers
Shaun Jackson
1,270 PointsGot it :)
<% if user_signed_in? %>
<ul class="nav pull-right">
<li class="active"><%= link_to "Log Out", destroy_user_session_path, method: :delete %></li>
<li><%= link_to current_user.full_name, edit_user_registration_path %></li>
<% end %>
</ul>
Alan Johnson
7,625 PointsSo when you get undefined method X for nil:NilClass usually the issue is that you're calling a method on a nil object, so it doesn't exist. In this case you're calling:
current_user.full_name
but current_user is nil. There are a couple of ways you'd usually work around that. The first is to make sure current_user is only called when someone is logged in (are you logged in?) and the other is to always check that current_user is present before calling it:
<li><%= link_to(current_user.full_name, "#") if current_user.present? %></li>
Shaun Jackson
1,270 PointsHi Alan,
Yes I was logged in and tried your method but got the following error:
SyntaxError in Statuses#index
Showing C:/Sites/treebook/app/views/layouts/application.html.erb where line #20 raised:
17: <li class="active"><%= link_to "Log Out", destroy_user_session_path %></li>
18: </ul>
19: <ul class="nav pull-right">
20: <li><%= link_to current_user.full_name, "#") if current_user.present? %></li>
21: </ul>
22: </div>
23: </div>
Alan Johnson
7,625 PointsAlan Johnson
7,625 PointsAwesome! Glad you figured it all out. Sorry about the syntax error there - I was tossing up possible solutions without having the project in front of me.