Author Topic: Displaying User Stats: You have a nil object...  (Read 850 times)

Offline atury

  • Level 4
  • *
  • Posts: 12
  • Reputation: +0/-0
    • View Profile
Displaying User Stats: You have a nil object...
« on: October 24, 2010, 05:29:04 PM »
I'm following along with the Ruby on Rails tutorials as a way to learn RoR faster. (I'm actually learning a lot more from those than the two books I have). Anyway, I'm following them and I'm on the part where you display the users stats on your welcome page after someone logs on. I have added the code as per the tutorial, only altered a little to suit my game:
Code: [Select]
<%= render :partial => 'users/user_bar' %>

<% if flash[:notice] %>
  <%= h flash[:notice] %>
<% end %>

<h1>Welcome To The Game!</h1>

<% if logged_in? %>
<h2><%= @current_user.login %></h2>
  <%= u.name rescue nil %>

<ul>Vital Stats:
  <li>Level: <strong><%= @current.user.level %></strong></li>
  <li>XP: <strong><%= @current.user.xp %></strong></li>
  <li>Health: <strong><%= @current.user.health %></strong></li>
  <li>Magic: <strong><%= @current.user.magic %></strong></li>
</ul>
<ul>Combat Stats:
  <li>Attack: <strong><%= current.user.attack %></strong></li>
  <li>Defense: <strong><%= current.user.defense %></strong></li>
  <li>Disengage: <strong><%= current.user.disengage %></strong></li>
</ul>
<% end %>

The problem comes with line 8:
[code]
<h2><%= @current_user.login %></h2>

It gives me an error: You have a nil object when you didn't expect it!

I don't know how to fix this, can anyone help?
[/code]

Offline Harkins

  • Level 28
  • **
  • Posts: 424
  • Reputation: +11/-2
  • Coder, blogger, entrepreneur.
    • View Profile
    • Push CX - Blog
Re: Displaying User Stats: You have a nil object...
« Reply #1 on: October 24, 2010, 10:38:12 PM »
Line 8 is complaining that @current_user is nil. So I'd look at that logged_in? function of yours, it must be returning true when no one is logged in. Either that, or the @current_user variable isn't getting set when someone's logged in.

Visit #bbg on irc.freenode.net to talk browser games anytime.

Offline atury

  • Level 4
  • *
  • Posts: 12
  • Reputation: +0/-0
    • View Profile
Re: Displaying User Stats: You have a nil object...
« Reply #2 on: October 25, 2010, 12:49:58 AM »
I also tested this out by removing the logged_in? portion and it still gives me the same error for all the stuff after the Vital Stats. So I'm wondering if it's the @current_user portion that's causing it. I don't know enough to know where to check or what to look for, as I'm just following the tutorials on this site.

Offline Harkins

  • Level 28
  • **
  • Posts: 424
  • Reputation: +11/-2
  • Coder, blogger, entrepreneur.
    • View Profile
    • Push CX - Blog
Re: Displaying User Stats: You have a nil object...
« Reply #3 on: October 25, 2010, 10:47:59 AM »
I'm not sure what tutorial you're following (got a link?) but in your ApplicationController or the specific controller for this page you should have a line that says '@current_user = ...'. Either that's missing or it's not getting called to log people in.

Visit #bbg on irc.freenode.net to talk browser games anytime.

Offline atury

  • Level 4
  • *
  • Posts: 12
  • Reputation: +0/-0
    • View Profile
Re: Displaying User Stats: You have a nil object...
« Reply #4 on: October 25, 2010, 11:58:48 PM »
I'm following the series on buildingbrowsergames.com. I didn't realize these forums were their own entity apart from that. Here is the link to the tutorial where the code originates (I believe):
http://buildingbrowsergames.com/2008/08/25/the-login-page-ruby-on-rails-part-2/

and here is the tutorial where the error happens:
http://buildingbrowsergames.com/2008/09/11/displaying-a-users-stats-ruby-on-rails/

From what I can tell, the @current_user is part of the restful_authentication plugin, which is introduced in the Login Page tutorial.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal