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

NoMethodError in Statuses#index

I am stuck at this part:

Programming Build a Simple Ruby on Rails Application Customizing Forms Creating Relationships

The error:

NoMethodError in Statuses#index

Showing /home/dinie/Projects/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 %>
Rails.root: /home/dinie/Projects/treebook

Application Trace | Framework Trace | Full Trace
app/views/statuses/index.html.erb:9:in `block in _app_views_statuses_index_html_erb___244668798192884438_30339620'
app/views/statuses/index.html.erb:7:in `each'
app/views/statuses/index.html.erb:7:in `_app_views_statuses_index_html_erb___244668798192884438_30339620'
app/controllers/statuses_controller.rb:7:in `index'

Any help would be appreciated. Thanks

4 Answers

does your app/models/user.rb contain the following code?

  def full_name
      first_name + " " + last_name
  end

That's correct, but the best way is the following:

def full_name
  "#{full_name} #{last_name}"
end

This way allows you to be less likely to have an error AND be able to be more flexible with new changes to the full name method you want to do in the future (such as like adding a middle name easily without extra + or "").

The error message is: undefined method `full_name' for nil:NilClass

So what it is saying is the object that it is calling full_name on (or in Ruby speak the more mind bending the object receiving the message full_name) is nil.

Look at line 9: status.user.full_name The most likely situation is that you have a status that has a nil user and when full_name method is called on that user object the system throws the error you are seeing.

That's da facts... now best guess given the facts is: Possibly you have at the start of the project created a status that didn't have an associated user? It didn't matter at the time but now it is causing a bug because your program expects all statuses to have users. If you have any old statuses I would delete them all and then start adding them again and see if you still get the error.

Rich

Nicholas Fox
Nicholas Fox
5,520 Points

I was stumped by the same error for a while. I kept clearing all statuses and users from the database using the rails console, then generating a new user and new statuses. I still would receive this same error message.

The issue was I was providing an incorrect user_id.

When you "sign up" for the site, look in the command prompt window and find the message associated with adding a new user to the database (you may have to scroll up). Near the end of that message, it will tell you the user_id associated with that user. Try plugging that in when writing a status. That fixed the problem for me!