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 trialWayne 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
10,865 PointsIn 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
7,796 PointsHi 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.
Josh Poulain
1,663 PointsAny luck with this?? I am getting the same error and having a hell of a time trying to figure it out.
Wayne Fonseca
7,796 PointsNo luck on my end. I think its a bug. I even downloaded the project files and found it has the same issue.
Anderson Mattjie
720 PointsHey, 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
9,737 PointsThanks heaps Anderson!
This was exactly my problem.
Clement Tay
1,876 PointsThanks 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).
Anderson Mattjie
720 PointsYou're welcome Clement. I'm glad I could help because when I passed through this situation I spent a whole weekend to solve it.
Jeffrey Jean-Pierre
7,079 Points3 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)
Jeffrey Jean-Pierre
7,079 Points3 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
7,796 PointsThank you @ Anderson Mattjie
Taige Zhang
1,068 PointsHey guys - I ran into the same issue and read what you wrote but didn't really understand it.
I typed
- rails console
- 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
4,142 Pointsor just go into your rails console and do a $ Status.delete_all and that will easily fix everything for you.
Daniel Barreto
Courses Plus Student 14,978 PointsFor 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?
Michael Leonard
866 Points@Daniel Barreto I am having the same issue. Any luck finding a solution?
Daniel Barreto
Courses Plus Student 14,978 PointsI 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.
Stephen Becker
7,056 PointsDaniel 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.