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

Ruby

Treebook ruby on rails tutorial

Hi,

I was just wondering if anyone has come across an error when following the 'treebook' rails tutorial:

main_project/treebook/app/views/statuses/index.html.erb where line #9 raised:

undefined method `full_name' for nil:NilClass
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 %>

I followed the tutorial all the way through up until after 'customizing-forms/adding-a-dropdown' and that's when I keep getting the same error message. I have cleared all statuses and gone into the console and deleted users etc. but I keep on getting the same error. I am new at rails and just trying to learn it so it's frustrating getting this error. I am just a newbie so maybe it's something really easy to fix?

10 Answers

I'm having the same problem! No idea where it came into play, I even have replaced code from the main project files to try and fix.

Please post a solution if you find it, I've been googling for 20 minutes hah.

Hi Scott,

I don't know where it came into play either. It's just weird it's happened but I can't really go any further in the tutorial now. I am just restarting the whole thing again just incase I missed something.

Joseph Hall
Joseph Hall
10,865 Points

Hi Guys, In you helper.rb file(where you store all you helper methods), you want to create a method called full name.

I think Jim does this in one of the videos but what you need to do is create the following line of code.

def full_name
    first_name + " " + last_name 
end

This code creates a method called full name, which just returns the first name + space + last_name of the user.

Creating helpers methods like these in Ruby is part of the DRY principal (Don't Repeat Yourself). If you wanted to display the users name all over the page, it would be a lot to type first_name + last_name. Instead you are creating a method, so you dont have to repeat yourself.

Regards, Joseph

Yeah he had us do that in the "user.rb" file before and (at least for me) it finds full_name fine on every page other than the index.

I've replaced my code with his code even for both files with still no luck.

Hi Joseph,

Thanks and I did as you suggested but I keep getting the same error.

I put that method in the User.rb in models which was in the tutorial or that was how Jim did it anyway. I just found older forum posts and there have been other people getting the same error.

I will just try and continue doing the tutorial as it's great following along with it.

Alright Mark, I think I found the solution.

I reset my databases (which deletes all of the statuses and accounts...so do this at your own risk haha because it worked for me, but I'm not sure if it'll work for you) by entering "rake db:drop rake db:create rake db:migrate" into terminal with the rails server turned off.

Hope this helps!

Hi Scott,

Thanks Scott I am going to try that now and I'll let you know how it went but I have nothing to loose by trying lol or I can't make it any worse I guess.

Note: you will need to go to /users/sign_up directly to create a new user before the index will work.

hi Scott.

I tried that and it just worked so thanks.

James White
James White
6,159 Points

I think this is because you might have registered more than one user and you haven't associated the users with the user_id.

I had this same problem. I wound up deleting all users in the database, changing the offending code back to first name, creating a new user, associating posts with the new user, or just delete all posts. (I might have the this out of order.)

Then change the code back to full name.