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 Build a Simple Ruby on Rails Application Customizing Forms Creating Relationships

Andrew Johnson
PLUS
Andrew Johnson
Courses Plus Student 12,617 Points

Undefined method `user' for nil:NilClass

Statuses are being written to the database with a user_id value of "nil"

It appears as though everything is absolutely broken in Rails 4, at least in relation to the gems/solutions presented in the tutorial. I expected that to some degree, but not quite this much. Bummer. Adding notes within the tutorial about how to fix the issues you come across would be more than enough, since searching through this forum has only fixed a couple of my problems.

My github repo is here if that makes troubleshooting any easier.

4 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Statuses have nil values when you are making/editing them through forms because of the way Rails 4 handles mass assignments. It uses strong_params, while Rails 3 (from the videos) had a different system. A temporary fix would be including the protected_attributes gem in the Gemfile. This should allow you to use the code from videos properly. But in the long run, you should either learn how to use Strong Parameters (the default in Rails 4) or generate the whole application from scratch, this time using Rails 3, like this:

rails _3.2.6_ new treebook

(or any other specific version that you want to use in the project, this is just the example, make sure you use the underscores)

Andrew Johnson
Andrew Johnson
Courses Plus Student 12,617 Points

I have actually already included the protected_attributes gem, but unfortunately it seems to have been a bit of a bust. It did fix a couple of my problems, but then this one appeared. Haha. Oh well, I'll just download the project files and see if I can run the rails server in it and go from there. Thanks for the help!

EDIT: Just in case someone reads this thread and wants to try my solution of downloading the project files and just running "rails s" it totally doesn't work. Don't try it. Just rebuild it.

I might as well include some info on how I managed to make this happen. I had to first uninstall the rails gem, using

gem uninstall rails 

Then it listed the two versions I already had installed ( 4.0.4, 4.0.3 ) and the option to delete one or both of them. I deleted both, then installed rails 3.2.6 using the command:

gem install rails -v 3.2.6

This appears to have worked.

I had this error before when I was in this exact same stage. I can't remember much of how I solved it. But here is what is happening: your are calling 'first_name' on a user that's nil. by the way in index.html.erb, remove the @ sign on the <%= @status.user.first_name %>.

to proceed, I recommend removing .first_name on that snippet and simply keep it <%= status.user %> in index.html.erb and <%= @status.user %> in show.html.erb. you will be able to post statuses without a user associated with. I know this doesn't solve the problem. But in the next tutorials is where they implement usernames to show up instead of user_id. Right now you are selecting user id from a drop-down. and that's not ideal anyway. So why bother.

what were you trying to do when you got the error? ya gotta provide more info. can you post the file the error is pointing to. I got this error before and my problem was I called the user method on an object with nil value (I think I had zero statuses and did statuses.user). If that's the case, create a status for the user inside the console.

Andrew Johnson
PLUS
Andrew Johnson
Courses Plus Student 12,617 Points

I am trying to create statuses, but the user ID I assign in the box isn't added to the database along with the status' content. I'm on the "creating relationships" step of the Simple Ruby App course, and upon adding the <%= @status.user.first_name %> line to views that display my statuses. The error is throws is the one I entered as the subject of my post, sorry if that wasn't clear.

So, if I wanted to create a status with a user-id in the database directly, I could, but that doesn't make my form work any better.