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

Tim Smith
Tim Smith
11,164 Points

nil:NilClass (ruby 4.0.0)

Hi Everyone,

I have a problem in my Treebook Ruby on Rails (4.0.0) app that I can't seem to figure out. I am at the part of the app development where they are introducing Simple form and connecting user_id with the user's name:

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

Everything works in my app the way it does in the tutorial except for the user_id itself. When I use the simple form to enter the user_id for a status, it is not saved when I save the status. I end up getting the error:

undefined method `first_name' for nil:NilClass

when I try to use <%= status.user.first_name %>.

I am not able to proceed with the course until this bug is resolved.

Hope this is a good enough description.

Thanks,

Tim Smith

3 Answers

Tim Smith
Tim Smith
11,164 Points

FIXED!!

I added :user_id to

def status_params params.require(:status).permit(:content, :user_id) end

in statuses_controller.rb.

Rails 4 handles things differently

Hillie Kootstra
Hillie Kootstra
3,136 Points

Hey Tim,

I tried this but it didn't work for me. Do i need to do anything else? Do a migration or something?

Tim Smith
Tim Smith
11,164 Points

After doing:

rails c Status.first

I get:

Status Load (0.1ms) SELECT "statuses".* FROM "statuses" ORDER BY "statuses"."id" ASC LIMIT 1 => #<Status id: 1, content: "Test", created_at: "2013-10-17 01:59:05", updated_at: "2013-10-17 01:59:05", user_id: nil>

It seems like my migrations are not running because I do not see a "user_id" column

Lisa Rossiter
Lisa Rossiter
3,630 Points

Hi Tim,

I know this is random but did you have to add permitted values for devise as well.

I also assume you have finished this tutorial now is there any problems you came across whilst doing this tutorial with rails 4.