Richlewis
Richlewis

Reputation: 15384

Clarification of When to use Controllers

Im new to rails and want to understand controllers better (especially when linked with models)

My example is I have Recipe controller and model and then a favourite controller and model. Within the recipe controller I have New, create, update, destroy etc, so that obviously I can perform those actions on a recipe.

Now i want to flag recipes as favourites, so again I can create a new favourite and delete a favourite. So is it best practice to put my requests for the favourite model in the favourite controller. Not in the same controller as recipe. the reason I ask this is because i have favourite and recipe associated with belongs_to.

Also within my app I want to view favourite recipes within the my_recipes page (this is in the recipe controller), does it matter that my create/delete methods are in another controller for favourites?

I hope this makes some sense to someone, looking for some clarification really

Upvotes: 0

Views: 26

Answers (1)

Thanh
Thanh

Reputation: 8624

This is two resources, so i think it would be better to have two controller for them. In your case, you can put your request in recipe controller, because you want flag favourite recipes. As you can see questions on stackoverflow has a start flag to make question is favourite or not. So you can put an action in recipe controller to make a normal recipe become favorite recipe. It means you can put create/delete methods not in favourite recipe controller.

Upvotes: 1

Related Questions