The controller and scaffold generators by default create view helpers (in
app/helpers) and I think this might have been the case since Rails’ inception. But is that a good/best practice in modern Rails applications? From my experience view helpers are often general and used across controllers/views.
Getting a helper per controller might also make it seem like the helper is only available for the similarly named controller which could lead to confusion over clashing method names.
Do folks here have view helpers closely related to their models/controllers?
It is already possible to configure your application to not generate helpers per scaffold. See Configuring Rails Applications — Ruby on Rails Guides
As for if that should be the default behavior, I don’t think so. I think it is good practice to group helpers specific for your controller in the same file. About them affecting all controller, there is also a config to disable that Configuring Rails Applications — Ruby on Rails Guides.
I am aware of those configuration options. The idea with my proposal is to reconsider the default behavior of generating view helpers because that doesn’t fit with what I have seen in the wild. It gives a nice symmetry to generate models, controllers and view templates+helpers in the scaffold generators but I think for 2 reasons view helpers feel like the odd one out:
- The helper methods are available globally by default while models, controllers and view templates are more local
- The other reason is, as mentioned, that in real world applications I often see view helpers be more general and be concerned with things like colors, mobile/desktop setups, formatting of dates and more. Granted I also see model/controller specific helpers but they would probably have been better suited if defined with
helper_method in the specific controller they are being used.
The decision of whether or not to generate a view helper by default might seem like a small detail but it does impact how Rails devs think they should build apps.