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! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 10 February 2010 18:54, RubyonRails_newbie <craigwesty79-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Can anyone see the obvious mistake? I could do with a fresh pair of > eyes on this... :-)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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Craig Westmoreland 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... :-)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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.. On 10 Feb, 19:12, Robert Walker <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Craig Westmoreland 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... :-) > > 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. > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I don''t see you defining @user anywhere. Which view is your code in? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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... On 10 Feb, 19:24, Steve Klabnik <steve.klab...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I don''t see you defining @user anywhere. Which view is your code in?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.... On 10 Feb, 19:45, Steve Klabnik <steve.klab...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Craig Westmoreland 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Craig Westmoreland 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 10 February 2010 19:54, RubyonRails_newbie <craigwesty79-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hmmm.. It''s still not working, even when I''ve added it in the invitations view page. :-( On 10 Feb, 20:02, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Craig Westmoreland 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-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 <craigwest...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Hmmm.. > > It''s still not working, even when I''ve added it in the invitations > view page. > > :-( > > On 10 Feb, 20:02, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > > > Craig Westmoreland 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-Koserhttp://www.marnen.org > > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
considering it''s user.invitation_limit, (or maybe @user), that means you need to define one or the other. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Yeah - I have @ user defined in the view and the controller.... On 10 Feb, 22:01, Steve Klabnik <steve.klab...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> considering it''s user.invitation_limit, (or maybe @user), that means you > need to define one or the other.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Udom Robot Mode LLC http://robotmo.de On Feb 10, 2:10 pm, RubyonRails_newbie <craigwest...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Yeah - I have @ user defined in the view and the controller.... > > On 10 Feb, 22:01, Steve Klabnik <steve.klab...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > considering it''s user.invitation_limit, (or maybe @user), that means you > > need to define one or the other.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
cool thank you. All valid points... I think i''ll go through the tutorial again. I may have missed something. cheers On 10 Feb, 20:20, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 10 February 2010 19:54, RubyonRails_newbie > > <craigwest...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.