Good Practice - Redirection in Models? - Rails 3.1

I have a model and controller Search. The business logic is that if the user's keyword exactly matches the product model number, redirect them to the product page.

In this situation, should I just redirect from within the model (where most of the logic already exists)?

Or do I need to return a flag or something to the controller in order to handle the redirection?

+5
source share
2 answers

The model object can never redirect. Application logic is the responsibility of the controller, so the controller must ask the model object (as a result of the request) if the product matches the model number, and then the controller redirects. The model object does not need to know anything about the controller or views. This is part of the Model-View-Controller Concept , which is implemented by Rails.

Rails implements the model as an ActiveRecord template , so it’s normal that the model object is responsible for the database and includes a search in the database. See Many Options in Rails Guides for ActiveRecord Queries to see what is the responsibility of model objects.

+9
source

MVC: MVC Rails

, . .

+4

All Articles