Hi all. I''m new to RoR and trying to figure out instance variables. I''m trying to put a string in one in the controller and print it in the view. However I''m getting the following error: NoMethodError in Say#hello You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.+ Apparently the @text variable remains nil, even though I assign a string to it. Any ideas about what goes wrong? This is my controller code: class SayController < ApplicationController def Hello @text = ''Hello world from the controller'' end def Goodbye end end This is my view: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/ TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Rails test (say)</title> </head> <body> <p>Hello world!</p> <p><%= ''testje '' + @text %></p> </body> </html> --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ar Chron
2008-Jul-24 16:35 UTC
Re: Instance variables created in controller are nil in view
Have you tried: <p>testje <%= @text %></p> -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It shows the text ''testje'', but there''s no output for the @text variable. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David A. Black
2008-Jul-24 17:41 UTC
Re: Instance variables created in controller are nil in view
Hi -- On Thu, 24 Jul 2008, Elegia wrote:> > Hi all. > > I''m new to RoR and trying to figure out instance variables. I''m trying > to put a string in one in the controller and > print it in the view. However I''m getting the following error: > > NoMethodError in Say#hello > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occurred while evaluating nil.+ > > > Apparently the @text variable remains nil, even though I assign a > string to it. Any ideas about what goes wrong? > > This is my controller code: > > class SayController < ApplicationController > def Hello > @text = ''Hello world from the controller'' > end > def Goodbye > end > endChange the method names to lower case. David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails July 21-24 Edison, NJ * Advancing With Rails August 18-21 Edison, NJ * Co-taught by D.A. Black and Erik Kastner See http://www.rubypal.com for details and updates! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---