Instance variables created in controller are nil in view

View: New views
5 Messages — Rating Filter:   Alert me  

Instance variables created in controller are nil in view

by Elegia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Instance variables created in controller are nil in view

by Jay Pangmi-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Instance variables created in controller are nil in view

by Elegia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Instance variables created in controller are nil in view

by David A. Black :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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
> end

Change 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@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Instance variables created in controller are nil in view

by Elegia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


It works! Thank you so much. I never would have thought about that.

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

LightInTheBox - Buy quality products at wholesale price