Nil object - can anyone help?

Hi there,

I’ve been looking for a while now and can’t seem to find where I’m
going wrong…

I’m following a railscast tutorial to send invites out to people.

there’s n invitation.rb page:
class Invitation < ActiveRecord::Base
#attr_accessible :sender_id, :recipient_email, :token, :sent_at

belongs_to :sender, :class_name => ‘User’
has_one :recipient, :class_name => ‘User’

###validation

validates_presence_of :recipient_email
validate :recipient_is_not_registered
validate :sender_has_invitations, :if => :sender

before_create :generate_token
before_create :decrement_sender_count, :if => :sender

private

def recipient_is_not_registered
errors.add :recipient_email, ‘Is already registered’ if
User.find_by_email(recipient_email)
end

def sender_has_invitations
unless sender.invitations_limit > 0
errors.add_to_base ‘You have reached your limit if invitations
to send’
end

def generate_token

self.token = Digest::SHA1.hexdigest([Time.now, rand].join)

end

def decrement_sender_count
sender.decrement! :invitation_limit
end
end
end

An invitations controller:

class InvitationsController < ApplicationController
def new
@invitation = Invitation.new
end

def create
@invitation = Invitation.new(params[:invitation])
@invitation.sender = user
if @invitation.save
flash[:notice] = “Thanks - Invitation sent successfully.”
redirect_to hub_url
else
render :action => ‘new’
end
end

end

And finally, the invite count, made visible to the user:
<% if @user.invitation_limit > 0 %>
<%= link_to ‘Send invitation!’, new_invitation_path %>

        <% end %>

THis page, for some odd reason will not render. Instead I get:
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.>

Can anyone see the obvious mistake? I could do with a fresh pair of
eyes on this… :slight_smile:

Thanks a lot!

Craig W. wrote:

THis page, for some odd reason will not render. Instead I get:
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.>

Can anyone see the obvious mistake? I could do with a fresh pair of
eyes on this… :slight_smile:

Have you looked at your stack trace to see where your app encounters the
nil object? The stack trace should direct you to the line in the file
causing the problem.

yeah - it’s complaining about the @user in

<% if @user.invitation_limit > 0 %>
        <%= link_to 'Send invitation!', new_invitation_path %>

        <% end %>

I’ve tried renaming it with just user, then User, but i can’t seem to
get my head around this…

Any ideas?

Many thanks…

I don’t see you defining @user anywhere. Which view is your code in?

my view is in the main layouts file.

I also tried to add @invitation.sender = @user to the invitations
controller but that hasn’t solved it…

in that case, would it make more sense to add it to a page when a user
is already logged in?

I’ve just tried adding it to the invitation’s index page, and i get
the same issue…

sorry…

Craig W. wrote:

my view is in the main layouts file.

I also tried to add @invitation.sender = @user to the invitations
controller but that hasn’t solved it…

Of course. It’s @user that’s coming up nil in the first place.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Your application.html.erb?

Then yeah, you’re going to need @user defined in every controller.

Also, is this a user that was created before you added in your
invitations?
If so, you won’t have anything for an invitation_limit for any of your
users, you’ll want to do a User.all.each{|user| user.invitation_limit =
5 &&
user.save } to give them the ability to send invitations. The
after_create
handles it for new users only.

Hmmm…

It’s still not working, even when I’ve added it in the invitations
view page.

:frowning:

Craig W. wrote:

in that case, would it make more sense to add it to a page when a user
is already logged in?

Heck yes.

I’ve just tried adding it to the invitation’s index page, and i get
the same issue…

sorry…

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

ok -

i’ve managed to get the following to render correctly (from within the
application.html.erb file:

        <% if @user.invitation_limit > 0 %>
        <%= link_to 'Send invite', :controller => 'invitations', 

:action
=> ‘new’ %>

        (<%= @user.invitation_limit %> left)

          <% end %>

However, when the link is clicked (send invite), I now get the error
mentioned before:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.invitation_limit

Any ideas where i need to make the change now, so this works?

Thanks again…

On 10 Feb, 20:21, RubyonRails_newbie [email protected]

considering it’s user.invitation_limit, (or maybe @user), that means you
need to define one or the other.

Yeah - I have @ user defined in the view and the controller…

Are you defining @user in application_controller.rb? I don’t see it
defined anywhere in the code you posted. If you’re using an
authentication plugin, you probably want to do something like:

@user = current_user

Even then, you’ll have to check to see if current_user is nil, unless
that page is only visible to logged in users. You can do this with
something like:

<% if @user && @user.invitation_limit > 0 %>

Also, in your create method you’ll want to do something like:

@invitation.sender = @user

or

@invitation.sender = current_user

Jarin U.
Robot Mode LLC
http://robotmo.de

On Feb 10, 2:10 pm, RubyonRails_newbie [email protected]

On 10 February 2010 19:54, RubyonRails_newbie
[email protected] wrote:

in that case, would it make more sense to add it to a page when a user
is already logged in?

I’ve just tried adding it to the invitation’s index page, and i get
the same issue…

If you’re following a Railscast, I don’t know where you may be going
wrong, because I’ve always found them to be very methodical.
It may be that you’ve missed a step, or put something in the wrong
place.

I’d suggest you backtrack, and follow the tutorial from the start
again - after all, they’re only 10mins long.

Also, inferring from your misunderstanding of the assignments and
variable scopes (and of course that you say you’re a Rails newbie),
you might benefit from refreshing yourself of some of basic principles
covered in the “getting started” chapter of your favourite Rails book.

cool thank you.

All valid points… I think i’ll go through the tutorial again. I may
have missed something.

cheers

On 10 February 2010 18:54, RubyonRails_newbie
[email protected] wrote:

Can anyone see the obvious mistake? I could do with a fresh pair of
eyes on this… :slight_smile:

In your view you’re accessing the instance variable @user, but you’re
not setting that anywhere in your controller (at least in the code
you’ve posted). So when your view renders, the @user variable is
nil… possibly the error.