DMWoodall/sealevelapp — pull request #1 by ralphos (2012-04-22 06:10:17+00:00)
Title: Late night cleanup!

Got the products form working.

There were a few mistakes in the code that I wrote.

i) I didn't have a foreign key on the products table (you have to manually input this - i assumed belongs_to would take care of it but it didn't).
ii) I added the wrong foreign key (categories_id) when it should have been category_id
iii) I cleaned up the products/new form (should probably put it in a partial now)
iv) I fixed the 'select' field in the form. It needed a 'collection' to display a list of categories so my syntax was wrong. You can see now the 2nd argument is Category.all... which is an array which we can map to iterate over. Ugly but does the job.
v) still need to build an association between products and categories in the form, cuz now when i create a product there is no way to tell 'this product belongs to this category'. (gotta check out the .build method in the Rails api).
vi) in create action in controller: @category = Category.create(params[:category]) works. We were calling the params hash wrong. You don't need to do @category = Category.create(:title => params[:category][:title] which assigns a string to :title. In the first way above, rails is smart enough to parse the hash params[:category] and take out the value of the :title key itself.

OK think I can sleep now after this little cleanup LOL. Gotta figure out out to build out those associations between products and categories. Should be fairly straightforward!