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

NoMethodError in Statuses#index

Hi All,

Just finished the Ruby on Rails video where you add the has_many :statuses section of code. The ruby page was working fine; however, after I defined "full_name" and inserted the following code:

<ul class="nav pull-right">
     <li><%= link_to current_user.full_name, "#" %></li>
</ul>

I get the following error page:


NoMethodError in Statuses#index

Showing /Users/pauldietrich/Documents/Projects/treebook/app/views/layouts/application.html.erb where line #18 raised:

undefined method `first_name' for nil:NilClass Extracted source (around line #18):

15: <li><%= link_to "All Statuses", statuses_path %></li> 16: </ul> 17: <ul class="nav pull-right"> 18: <li><%= link_to current_user.first_name, "#" %></li> 19: </ul> 20: </div> 21: </div> Rails.root: /Users/pauldietrich/Documents/Projects/treebook

Application Trace | Framework Trace | Full Trace app/views/layouts/application.html.erb:18:in _app_views_layouts_application_html_erb__2343450988773708573_70236940861300' app/controllers/statuses_controller.rb:7:inindex' Request

Parameters:

None


I'm not sure what's wrong.

5 Answers

I would try to reset your database

rake db:reset

I'm having the exact same error and haven't found a way to fix it. I tried rake db:reset, got an error, it recommended I do rake db:migrate, so I did that and it's still not working.

Anyone have any idea what's going on? I'm completely new to Ruby and this has me stumped!

Since you are not logged in, the current_user variable will be nil and that is why your application crashes. Wrap the li in a condition, so that it only displays when a user is logged in.

<ul class="nav pull-right">
 <% if user_signed_in? %>
  <li><%= link_to current_user.full_name, "#" %></li>
  <% end %>
</ul>

Thanks so much, Szabolcs! That did it.