I’m trying to get the following code to work, but it’s giving me an undefined variable error:
class PagesController < ApplicationController
@company = "Acme"
def home
@title = "Welcome to #{company}"
end
def faq
@title = "FAQ | #{company}"
end
end
How do I access @company within functions?
As in Ruby, every class is an object too, what you are doing here is setting an instance variable named @company on the class object PagesController, not on an instance of PagesController.
What you may want is to use a constant instead of the instance variable. Maybe like this:
If you want the company to change dependent of what “page” you are displaying, you should consider adding a Page model which has an attribute which holds the company name or an association to another model named Company.