Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Julian McNeill
Julian McNeill
9,444 Points

HELP!!! I keep getting undefined method `full_name' for nil:NilClass

I've entered everything right according to the lesson but i keep getting this error

Extracted source (around line #9):

6: 
7: <% @statuses.each do |status| %>
8: <div class="status">
9:   <strong><%= status.user.full_name %></strong>
10:   <p><%= status.content %></p>
11:   <div class="meta">
12:     <%= link_to time_ago_in_words(status.created_at) + " ago", status %>

this is my second time starting from scratch. I don't understand what I entered wrong. can one of the teachers please help cause i'm stuck.

2 Answers

I think you don't have a user object to call the method full_name on. If there is no user object then the object you're calling the method full_name on is going to be nil. Which explains why you're getting a no method error for nil. Your full_name method is defined for your User class not Not Nil class.

Did you create a user, then create a status, then delete the user that created that status? I think this might work to get rid of the error but you would want to figure out why exactly this is happening.

Replace line 9 with this-

<%= status.try(:user).try(:full_name) %>

Not entirely sure you need that second try in there, try it both ways!

-twiz

Julian McNeill
Julian McNeill
9,444 Points

Thank you the code you gave me worked. how did you come up with that? I'm wondering why the code that was in the lesson wasn't working?

Hey Julian here is where you can read up the try method http://apidock.com/rails/Object/try. Basically, instead of raising the error that you got above the it will evaluate to nil. This still means there is something up with the data. The code from treehouse should work but somehow a User may have gotten deleted.

For example, imagine 3 people lined up in a row facing you and each person has a button on their forehead that is called full name. Each time you press the button on their forehead the person says their full name out loud.

So, you press the button on the forehead of the first person and the person says, "Julian McNeil." Ok good. Then you press the button on the forehead of the second person and the person says, "Sally Reynolds." Good again. When you get to the 3rd person, there is no person. Just a button on the floor that says full name. You press the button anyways. Nothing. How can that person say their full name if there is no person? That 3rd person is nil. And you don't have method or button full_name defined for nil, only for people or Users, so you get the no method error.

twiz

Julian McNeill
Julian McNeill
9,444 Points

got it thank you for explaining

Rick Champlin
Rick Champlin
11,072 Points

I used this answer to fix a problem I was having, but now it created a new one for me.

undefined method `+' for nil:NilClass

Line 15

<div class="field">
       <%= f.input :user_id, collection: User.all, label_method: :full_name %>
       <%= f.input :content %>
     </div>
     <div class="form-actions">

Please help!

Rick Champlin
Rick Champlin
11,072 Points

I was having the same error and this fixed it...but now I have a new one.

undefined method `+' for nil:NilClass

     <div class="field">
       <%= f.input :user_id, collection: User.all, label_method: :full_name %>
       <%= f.input :content %>
     </div>
     <div class="form-actions">

It is still a problem with the :full_name issue.

Please help

We can solve that by the following create migration to give first_name and last_name a default value of " ".

def full_name first_name.to_s + " " + last_name.to_s end

that because ruby will give you an error if you have "nil +". then we need to run "rails generate migration " in the terminal if you are using Mac or CMD in windows.