Why do we need helpers in Ruby on Rails? Why it is impossible to define them in application_controller?
2 answers
The fact is that, yes, helper methods will be available not so much in the controller as in the view, i.e. these are his methods, and he was created rather to unload the view from the cumbersome code. To make the methods available in the controller, you need to declare an assistant there forcibly. In case you implement the helper methods explicitly in the controller and in the view, you, as Fatbel correctly noted, simply make the code unreadable. In general, if we speak for the "rail path", then a more optimal scheme is as follows:
- controller - processes the input request data and passes the necessary parameters (see params_permit);
- model - in most cases, describes the requirements for table fields in the database (see validates), relationships between tables (see belongs_to, has_many / one) and samples (see scope) of data from tables by conditions. There may also be models without reference to a specific table, for example. abstract patterns. In this case, the properties of this simply set some common basis for the models of heirs.
- presentation - provides a description or more precisely the scheme of what the end user will see as a result of his request. Views are usually described by templates erb, haml, slim, etc.
Additionally, it is desirable (and in fact often necessary) to have:
service (service) - allows you to unload the controller from the business logic, each service usually represents some kind of operation on models with the possible involvement of other services. Services can be written by hand, and you can use beads services .
decorator — allows you to unload the model, and often the helpers, from redundant view logic, for example, from often called in a view when displaying a record of methods that usually convert any kind of data to output. Here draper beads are commonly used.
serializer (issuer, serializer) - allows you to unload the controller and model from business logic, and the sample that is needed by the end user and requested via api-calls (meaning external serialization). Commonly used beads are active_model_serializers .
- The model may not be associated with the database. Relatively rare, but it happens. - D-side
- @ D-side yes, of course, for example, abstract models =) - Mal Skrylev
- For example, yes. And still there are models representing representation (oops) from a DB. Or other entities, for example, OS processes, git branches. To some extent, the concept of database can be pulled on them, but this will only clarify what is meant by database in the context of architecture. - D-side
- @ D-side Well, you can shove anything into the model, but it won't be rails-style either =) - Mal Skrylev
They can be defined and there through helper_method. Only this is advisable for a very narrow circle of helpers that work literally on all pages. Otherwise, this is a crap of the controller and never a "Rails way".