Cosmin Atanasiu
Cosmin Atanasiu

Reputation: 2664

Why is my instance variable undefined in HAML view

I'm not sure why I couldn't find this in the documentation or anywhere online, but if my controller is:

# HomeController
class HomeController < ApplicationController
   def index
      @var = "A Sample Variable"
   end
end

..and if my HAML view uses a haml layout and looks like this:

/ index.html.haml
%p "This is a paragraph"
%p
   = @var

...why is the instance variable @var is never shown? The paragraph is there but @var is always null. I know this because calling @var.split('') throws an undefined method error. The HAML version I'm using is 4.0.3. Any ideas ?

Upvotes: 1

Views: 995

Answers (2)

dodgerogers747
dodgerogers747

Reputation: 3345

Change the view file name to index.html.haml as that's the action you have your instance variable defined under. Or alternatvely you can add a home action to your controller and store the var instance variable there, so the home template has access to it

Upvotes: 1

Chris Heald
Chris Heald

Reputation: 62698

Because your index action will render index.html.haml, not home.html.haml. You're rendering the wrong view there.

Upvotes: 2

Related Questions