Topic: Multiple Controller Question

Being new to Rails I am wondering the best or right way to do the following:
I have products that are grouped into multiple categories and I want to display all the products from all categories in one list and then I want to have a separate page for each and every other singular category to display all products from within that category.  I was wondering is it best to have 5(or how ever many different categories there are) different controllers that are then supplied the data such as AllCategories, Category1, Category2, Category3 etc...
I hope my question makes sense. Thanks for all help in advance.

Re: Multiple Controller Question

I'm assuming you have two models? Category and Product where category has_many :products? I recommend creating two controllers: "categories" and "products". You can generate these with scaffolding to give you some code.

The index action in each controller would list all of the models - so this is how you could list the categories and products. When you click a category this can go to  the "show" action of the Categories controller which will give details about that category. Here you can list all of the products in  the category like this:

# show.rhtml
<% for product in @category.products %>
  <%= product.name %>
<% end %>

Does that help?

Railscasts - Free Ruby on Rails Screencasts

Re: Multiple Controller Question

I actually only have one admin type controller that allows you to enter all the data into for the product, this was created using scaffolding. From here you select the category, year, make etc...  Did I start off incorrectly by doing this?

Re: Multiple Controller Question

Sounds like you already have the "products" controller implemented then. How are you managing the category model (adding categories, editing them, etc.)?

If you would like to stick with one controller that's okay, but I prefer to split controller's based on the models they manage. Any controller could handle the job of listing products in a certain category, but it's just my opinion it belongs in the controller which manages the category model. In the end though, the best way to organize controllers depends largely on the interface.

Railscasts - Free Ruby on Rails Screencasts