Im trying to define a select tag in my view.
My view looks like this:
<div class="field">
<%= f.label :analyst %><br />
<%= select_tag :sub_category_analyst, options_for_select(analyst_names, :selected => @sub_category.analyst || 0) %>
and my model looks likes this:
@@analyst_names = ["", "foo", "bar"]
belongs_to :category
has_many :products
def analyst_name
@@analyst_names[analyst.to_i || 0]
end
def self.analyst_names
@@analyst_names
end
The error when trying to load the page is: undefined local variable or method `analyst_names’
Thanks in advance!
I don’t know what your model is called, so I’ll call it
MyModelfor now.In your view, you’re going to want to replace
analyst_nameswithMyModel.analyst_names, because otherwise it thinksanalyst_namesis a local variable in the view, and then it can’t find that variable.You might benefit from reading about scope (some links about it are here and here).