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

Lee Hughes
Lee Hughes
8,380 Points

NoMethodError in Statuses#show

I've been stuck on this for a few hours now and I've gone through similar forum posts on this topic but nothing I seem to try works.

My code is fine, I got so frustrated by this error that I downloaded the project files and copied them over to make sure I didn't miss anything.

Here's the Github https://github.com/leehughes/treehouse

The error I'm getting is

undefined method `full_name' for nil:NilClass

This is on line 5:

2: 
3: <p>
4:   <b>Name:</b>
5:   <%= @status.user.full_name %>
6: </p>
7: 
8: <p>

I know the error is coming up because there's no relationship between user and full name.

I've tried deleting all pervious users by doing a

rake db:reset

But I'm totally confused on why I'm still getting the error given that the code, rails versions are both correct.

Any advice?

12 Answers

Alrighty. The problem is that you don't have a user with id = 1, but when you created a status, that is what was saved (Notice when we called Status.last previously the user_id was 1). My suggestion is to delete all statuses, delete all users, and recreate a couple users and try again.

Lee Hughes
Lee Hughes
8,380 Points

Awesome!!

I ran

Status.delete_all

then

User.delete_all

Restarted my server, no more errors!

I can't thank you enough, I spent all morning trying to figure that out!

Yeah. what happens is that you created a bunch of statuses, then you ran a migration to add the user_id column. Thus those old statuses didn't have the user_id. Sometime it's best to just get a clean start on the database as you learn.

Definitely play with the rails console. It will become your best friend to access the database.

Lee Hughes
Lee Hughes
8,380 Points

It was a good learning experience tbh.

I thought the rake db:reset would have done a similar thing but I guess not.

Thanks again :)

@status.user is not making it into the database. If you are using rails 4, it's because you need to allow user_id using strong_parameters in your statuses controller. Search the forums, this topic has come up many times.

Lee Hughes
Lee Hughes
8,380 Points

I'm using rails 3.2.14.

I've also researched the forum and tried various suggestions but still struggling to fix the error.

What's in your statuses controller? Particularly the create action? Also, paste your status.rb model here.

Lee Hughes
Lee Hughes
8,380 Points

This is the current version:

https://github.com/leehughes/treehouse/blob/master/app/controllers/statuses_controller.rb

status.rb

class Status < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user

end

In your _form.html.erb file, you have a typo:

 <%= f.input :user_id, colection: User.all, label_method: :full_name %>

Collection is spelled wrong.

Could you create a status and then go to rails console and type Status.last and paste the output here so we can see how it's being entered into your database.

Lee Hughes
Lee Hughes
8,380 Points

Thanks for that, I didn't notice that.

Here's the output:

 Status Load (0.1ms)  SELECT "statuses".* FROM "statuses" ORDER BY "statuses"."id" DESC LIMIT 1
 => #<Status id: 2, content: "hello", created_at: "2014-05-06 22:29:06", updated_at: "2014-05-06 22:29:06", user_id: 1>

And it's still giving you an error?

Lee Hughes
Lee Hughes
8,380 Points

Yup, I can't access the following pages:

0.0.0.0:3000/statuses

Extracted source (around line #9): in the file app/views/statuses/index.html.erb

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 can access 0.0.0.0:3000/statuses/new and create a new status

When I do that I get another another error

0.0.0.0:3000/statuses/4

Extracted source (around line #5): app/views/statuses/show.html.erb

Have you tried restarting your server. Ctr + C, then rails s and refresh the page.

Ok, time for some more rails console. I think the error is in your user maybe. Go to the console and type

User.find(1)

Paste that output here.

While you are at it, play in the rails console. Let's see if this works:

S = Status.find(2)
S.user
S.user.first_name
S.user.last_name
S.user.full_name
Lee Hughes
Lee Hughes
8,380 Points

for User.find(1)

User Load (2.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
ActiveRecord::RecordNotFound: Couldn't find User with id=1
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/activerecord-3.2.14/lib/active_record/relation/finder_methods.rb:344:in `find_one'
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/activerecord-3.2.14/lib/active_record/relation/finder_methods.rb:315:in         `find_with_ids'
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/activerecord-3.2.14/lib/active_record/relation/finder_methods.rb:107:in `find'
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/activerecord-3.2.14/lib/active_record/querying.rb:5:in `find'
from (irb):1
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

I'm getting similar errors for

S.user.first_name

NoMethodError: undefined method `first_name' for nil:NilClass
from (irb):4
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

S.user_last_name

NoMethodError: undefined method `last_name' for nil:NilClass
from (irb):5
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

S.user_full_name

NoMethodError: undefined method `full_name' for nil:NilClass
from (irb):6
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p247@gemset/gems/railties-3.2.14/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'