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 trialamo100
7,260 PointsName not coming up?
When I add @status.user.first_name to show.html.erb the name doesn't come up? I am using Rails version 3.2.19 and the attr_accessible is working. Any help would be appreciated. Thanks
Kang-Kyu Lee
52,045 PointsYes, with code we can find more exact answer. However how about try & test in rails console?
amo100
7,260 PointsHi I have put up my code on github the link is https://github.com/amo100/treebook. I am unable to get my name on to treebook as :firstname is not being recognised..
2 Answers
Maciej Czuchnowski
36,441 PointsI downloaded your code, did bundle install
and rake db:migrate
, started the server, went to registration page, registered as John Doe, created a new status as user 1 (since this is the user_id of John Doe that I just registered) and it works fine - shows Name: John on the status show page (throws errors when I try to publish as a non-existent user or without the user_id). Maybe try purging your whole database in the development environment, register a new user and see if it works from now on? Or your user has an empty first_name field? As far as I remember, this course was a bit chaotic at this point and I had lots of problems myself, but the code seems fine and it works as expected on my machine.
When I created a new user with only email and password (no first_name - there are no validations on your user model), it did publish posts with empty name field, so my guess is that this is your situation - you are posting as a user that has empty first_name field.
amo100
7,260 PointsThanks Maciej the name now comes up on the show page, however when I put <%= @status.user.first_name %> on the index page, it comes up with an error as it is not recognizing the user method...
NoMethodError in Statuses#index
undefined method `user' for nil:NilClass.
Did you experience a similar problem? If so any help would be great...
Maciej Czuchnowski
36,441 PointsCheck your controller and its index action. It has the instance variable @statuses
, not @status
. What is more, the index template has this line:
<% @statuses.each do |status| %>
So you'd actually have to use this in your index page:
<%= status.user.first_name %>
(notice: no @ symbol, because it has to use the local status
variable.
I imagine this all sounds a bit confusing right now, but the more you work with it and create more apps on your own, the clearer and more logical it will become.
Kyle Daugherty
16,441 PointsIt's hard to know for sure without seeing your code, but make sure you include the "=" sign in your erb tag. For example:
<%= @status.user.first_name %>
Maciej Czuchnowski
36,441 PointsMaciej Czuchnowski
36,441 PointsWe can't help you until we see your code. Please post relevant code here or - even better - put it on github and link it here.