Hi,
I''m using a "collection_select" form-helper in a view:
<%= collection_select(''product'',
''sub_category_id'', @sub_categories,
''id'', ''name'') %>
In the appropriate action in the controller I also have:
@sub_categories = SubCategory.find_all
However, I get a NoMethodError and the message:
WARNING: You have a nil object when you probably didn''t expect it!
Odds are you
want an instance of Array instead.
If I move the line that populates @sub_categories directly into my view, to get:
<%= @sub_categories = SubCategory.find_all %>
<%= collection_select(''product'',
''sub_category_id'', @sub_categories,
''id'', ''name'') %>
the view works and I get the selection list I expect.
Can anyone explain to me why it works in the view but not in the
controller? Everything I''ve read seems to say that the @sub_categories
= SubCategory.find_all line should be in the controller.
Sorry for what is undoubtedly a simple question but I really don''t
seem to be able to find an answer to this elsewhere.
Cheers! :)
--
Matthew Revell
www.understated.co.uk