Instance variables created in controller are nil in view

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:

Rails test (say)

Hello world!

<%= 'testje ' + @text %>

Have you tried:

testje <%= @text %>

It shows the text ‘testje’, but there’s no output for the @text
variable.

Hi –

On Thu, 24 Jul 2008, Elegia wrote:

You might have expected an instance of Array.
@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!

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs