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…
Thanks a lot!