Matthew Revell
2005-Sep-22 08:03 UTC
Populating an array - put code in view or controller?
Morning all, I''ve come across a frustrating and somewhat odd problem. If I put the code: @sub_categories - SubCategory.find(:all) into a form partial, @sub_categories works when used in a collection_select form helper. However, if I put that code in the controller, I''m told given a "You have a nil object..." error message. I reckon the code belongs in the controller, rather than the partial but, no matter how many examples I look at, I don''t seem able to fix the problem. The relevant part of my controller is simple enough and as follows: class ProductController < ApplicationController model :SubCategory def edit @product = Product.find(params[:id]) @sub_categories = SubCategory.find(:all) end end My partial looks like: <%= error_messages_for ''product'' %> <!--[form:product]--> <p><label for="product_name">Name</label><br/> <%= text_field ''product'', ''name'' %></p> <p>Category<br /> <p><label for="sub_category_name">Sub-category</label><br/> <%= collection_select(''product'', ''sub_category_id'', @sub_categories, ''id'', ''name'') %> </p> <p><label for="product_created_at">Created at</label><br/> <%= datetime_select ''product'', ''created_at'' %></p> <p><label for="sub_category_updated_at">Updated at</label><br/> <%= datetime_select ''product'', ''updated_at'' %></p> <!--[eoform:product]--> I feel as though I must be missing something pretty fundamental about Rails or Ruby. I''ve tried looking for examples of other people having similar problems but I''m not entirely sure what terms to search for. Thanks in advance, if anyone''s able to offer some advice. -- Matthew Revell www.understated.co.uk
Mark Reginald James
2005-Sep-22 12:08 UTC
Re: Populating an array - put code in view or controller?
Matthew Revell wrote:> model :SubCategoryThis line is probably not needed, and if it is it should refer to the model filename rather than the class name: model :sub_category -- We develop, watch us RoR, in numbers too big to ignore.