One question about Dynamic Select Menus

Hi all,
Merry X’mas first :slight_smile:

I have “Product”,“Category”,“Subcategory” 3 Model, and when new
“Product”, I want to use “Dynamic Select Menus” just like Ryan
mentioned in #88 Dynamic Select Menus - RailsCasts

I have everything down. But when I visit the js file, I got following
error

Showing app/views/javascripts/dymanic_categories.js.erb where line #3
raised:

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.each

Extracted source (around line #3):

1: // javascripts/dynamic_categories.js.erb
2: var subcategories = new Array();
3: <% for subcategory in @subcategories -%>
4: categories.push(new Array(<%= category.id %>, ‘<%=h
subcategory.subcategory_name %>’, <%= subcategory.id %>));
5: <% end -%>

Anyone got an idea, how does it happen?

What does the controller set @subcategories to before rendering the
javascript?

–wpd

Hi Partrick,
Before the controller rendering the Javascript,
@subcategories = Subcategory.find(:all)

Showing app/views/javascripts/dymanic_categories.js.erb where line #3
raised:

“dymanic”?

Hey, Eric
Thank you very much for help.
I checked my codes again, yes I made a typo here, it should be
“dynamic_categories” here
And now when I visit app/views/javascripts/dynamic_categories.js.erb I
can get the array

But, question comes again :frowning:

I can get category and subcategory list in my item/new view
But, when I change the value of category the value of subcategory
doesn’t change!

I checked with web inspector, I got this error message"TypeError:
Value undefined (result of expression document.observe) is not
object."
It seems my result of document.observe is not correct?

How can I fix it?

OK, guys
Finally I figured it out.
I missed the <%= javascript_include_tag :defaults %> in my layout.
Now everything goes fine.
Thank you very much for your help.