Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Wayne Fonseca
Wayne Fonseca
7,796 Points

[Ruby Rails Issue] <%= status.user.full_name %>

Issue:

undefined method `full_name' for nil:NilClass
5: <%= link_to "Post a New Status", new_status_path, class: "btn btn-success " %>
6: <% @statuses.each do  |status| %>
7: <div class="status">
8:   <strong><%= status.user.full_name %></strong>
9:   <p><%= status.content %></p>
10:   <div class="meta">
11:     <%= link_to time_ago_in_words(status.created_at) + " ago", status %>

Solution:

I suspect it has something to do with index.html.erb

<strong><%= status.user.full_name %></strong>

Any clues?

14 Answers

Joseph Hall
Joseph Hall
10,865 Points

In your helper method helper.rb file,

write this will tell index.html.erb that you are creating a helper method called full_name, which returns the first name + space + last name of the user

def full_name first_name + " " + last_name end

Wayne Fonseca
Wayne Fonseca
7,796 Points

Hi Joseph,

I am assuming that the helper.rd refers to app\models\user.rb? I have written the code:

   def full_name
   first_name + " " + last_name
   end 

I have also downloaded the project to refer to my codes, still the same issue.

Any luck with this?? I am getting the same error and having a hell of a time trying to figure it out.

Wayne Fonseca
Wayne Fonseca
7,796 Points

No luck on my end. I think its a bug. I even downloaded the project files and found it has the same issue.

Hey, I've just passed through a similar situation, but the error was:

undefined method 'first_name' for nil:NilClass

The problem was that I associated with the status the ID of an user that I had deleted, so certainly It wouldn't return first_name and etc.

Try to go to rails console, type in "u = User.first" to see the ID of this user, log in with this user and when you create the status, type in the ID of this user.

Hope it helps.

Sebastian Hirsch
Sebastian Hirsch
9,737 Points

Thanks heaps Anderson!

This was exactly my problem.

Thanks Anderson! That helped.

I ran the command on rails console and I realized user id 1 did not exist (Which was who the status belonged to).

You're welcome Clement. I'm glad I could help because when I passed through this situation I spent a whole weekend to solve it.

3 days for me...was getting frustrated, but I also realized that my databases weren't creating the tables it needed to. so I deleted the schema.rb file and did another migration and voila.

this was After I added the Rails 4 version strong_params (vs the previous way using protected_attributes)

3 days for me...was getting frustrated, but I also realized that my databases weren't creating the tables it needed to. so I deleted the schema.rb file and did another migration and voila.

this was After I added the Rails 4 version strong_params (vs the previous way using protected_attributes)

Wayne Fonseca
Wayne Fonseca
7,796 Points

Thank you @ Anderson Mattjie

Hey guys - I ran into the same issue and read what you wrote but didn't really understand it.

I typed

  1. rails console
  2. u = User.first and then got

User id: 3, first_name: "Jim", last_name: "Joke", profile_name: "JJ", email: "jim@joke.com", encrypted_password: "$2a$10$69xQikPELpFh2gMo0.Cv6O602LiJGSb4ve6VeBFspnLL...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2013-07-10 17:01:22", last_sign_in_at: "2013-07-10 17:01:22", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2013-07-10 17:01:22", updated_at: "2013-07-10 17:01:22"

So I didn't know what that means or how to fix it. Can you explain?

On a side note, I resolved the issue a different way by going to index.html.erb and taking out <strong><%= status.user.full_name %></strong>

and deleting all the statuses and then putting that back. Now app seems to work.

Sean Huntington
Sean Huntington
4,142 Points

or just go into your rails console and do a $ Status.delete_all and that will easily fix everything for you.

Daniel Barreto
PLUS
Daniel Barreto
Courses Plus Student 14,978 Points

For me, when i pull up the users in the rails console, it shows

User id: 2, first_name: nil, last_name: nil, profile_name: nil, email: "daniel.barreto717@gmail.com", 

for some reason when I submitted the form it did not save my input and I can't edit these fields when I go to localhost:3000/users/edit. Any ideas on how to fix this? Would i add attr_accesible to the edit page?

@Daniel Barreto I am having the same issue. Any luck finding a solution?

Daniel Barreto
PLUS
Daniel Barreto
Courses Plus Student 14,978 Points

I wish I could remember lol. I haven't touched that project in a while. Was actually thinking about going through it again from the beginning. If I come across it again, ill update the thread.

Daniel Barreto I'm having the same issue as you were. I'm trying to figure out who to get those stored values in the database, when they are created via the form.