Rails heavily depends on the model view controller (MVC) design pattern. Every one of them has a specific job and jeopardizing them is never a good idea. A controller is responsible for handling Rails requests and passing data from a model to a view. You can think of the controller as a manager between the logic of your program and the actual view that a user actually sees. Generally, a controller should have very few code. It should just execute some functions and retrieve instance variables to be used directly in a view. Moreover, it is used to do redirects with redirect_to and the likes.
A model is where your actual business logic is. The body of your main functions should always lie inside a model that is responsible for handling that data. Since a model operates on data, it’s pretty sensible that a model actually represents a database table and the operations that can be done on that. Always make sure that your functions and core code is inside your models.
A view is where data is represented to the user. You should never (really, never) include logic inside your views. If you feel that you need to include some sort of code inside your views, chances are high that you will be executing more database queries than actually needed. If you feel that you can use a hash instead, do it; although a bit more code to write, it’s the superior choice.
Some people may believe that a helper is the way to elegantly include code in your views. However, that is not really the case. A helper is actually (or should be) sort of a formatting underlying task. For instance, suppose that you have a hash that contains the costs of 4 different products, that you need to present to your view. If you would like to present some of the prices in euros and some in dollars, you could use a helper that would create a string like “this costs 20 dollars” or a string like “this costs 18 euros”, based on the helper function input.
Always put code having this priority in your mind :
-
model
-
helper
-
controller
-
view