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

Show First Name

I am trying to follow along with Create A Simple Ruby App. and I am on creating relationships but when I put "<%= @status.user.first_name %>" I receive
undefined method `first_name' for nil:NilClass? I have no idea what todo.

4 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

Sounds like the user is not being saved to the status. Try creating a new status with a user that has a first name. You will also need to clear old statuses from your database.

Search the forums here, it's been answered many times before.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

I'm having the same problem and none of the forum solutions seem to work. For some reason (maybe due to some changes introduced between the date of the recording and today, in Rails, Devise etc.) first_name, last_name and profile_name never get any values assigned (they are nil), no matter how many times you try to edit the user, even when I created a fresh new project and copied the project files from the lecture.

Brandon Barrette
Brandon Barrette
20,485 Points

Sounds like you are using Rails 4. I would research attr_accessible in rails 4.

You have to permit the values to be stored in the database, if they are not permitted, rails won't let them through and this won't save. This sounds like what's happening.

I highly suggest using rails 3.2 instead and learning with the videos. It will save you many headaches.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Yeah, I guess it's the only way. attr_accessible is not available in Rails 4 by default, I had to add gem 'protected_attributes' to Gemfile in order to be able to use it. Still, this did not help much, so I guess more needs to be done here. I will do some more research, thanks for pointing that out.

Brandon Barrette
Brandon Barrette
20,485 Points

Well don't use attr_accessible, but instead research it's replacement. Learn how that works first. I wouldn't use the protected_attribues gem.

You will have to modify your code but that's my guess as to why it's not working.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, I have SOME progress. I read a few websites on the new Strong Parameters and the way Devise handles them. I added some code to the application controller:

```before_filter :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) << :first_name devise_parameter_sanitizer.for(:sign_up) << :last_name devise_parameter_sanitizer.for(:sign_up) << :profile_name devise_parameter_sanitizer.for(:account_update) << :first_name devise_parameter_sanitizer.for(:account_update) << :last_name devise_parameter_sanitizer.for(:account_update) << :profile_name end```

(there's probably a more efficient way to do that)

This caused First Name, Last Name and Profile Name to be saved in the database, so now I can see those fields filled when editing user and when inspecting user database within Rails console. But undefined method `first_name' for nil:NilClass is still there whenever I want use `<%= @status.user.first_name %>.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

And I can't edit my comments now, because this forum is unstable...

Brandon Barrette
Brandon Barrette
20,485 Points

Are you sure "user" is being saved to status? Go into rails console and check that each status has a user. If not, this error will show because status.user will be nil, and calling first_name on nil returns the error.