This chapter explains points what you should pay attention related display.
ERB / Slim / Haml
Slim or Haml can be used to less code, but ERB is often chosen for collaboration with frontend engineers or designers.
Helper
It is used to separate the business logics from Views for easier to read. However, some people don't like to use Helper because it makes responsibilities ambiguous. For example, it may be included in the Controller, or it may be a Fat helper has a lot of logic. These may be the anti-pattern in Rails.
However, if there are the following simple rules, Helper can be used without problems.
- Prohibit the use of include in controller (and other places).
- Separate Model-related logics from Helper to Decorator.
Decorator
It can separate display logic from Models or Views to Decorators. If Decorators are used, place the following.
Since this is not a standard function, the gems such as active_decorator
or draper
are used. I recommend to know one of them what it is like.
active_decorator
Create Decorator such as app/decorators/{model_name}_decorator.rb
and define display methods in it, so that @model_instance
in the Controller will be automatically included Decorator and will be available Decorator methods in the View.
There are examples.
Github - active_decorator#examples
If you want to use Decorator methods in Controllers, Models or RSpecs, can use ActiveDecorator::Decorator.instance.decorate(model_instance)
to be available.
How to use with helper
Github - active_decorator#features
The description says that it can also generate HTML tags such as content_tag
and link_to
. So, the basic usage is like the following.
- Place the Model-related logics in Helper.
- Place the not-Model-related logics in Decorator.
form_with / form_tag / form_for
Use form_with
to submit HTML form. It can be used whether it is tied to a model or not. It is better to know what options are available.
Note that form_tag
and form_for
are deprecated.