I have some nice lean controllers. To further DRY them up, I would like them to share views: so that multiple controllers use the same edit and new views. So I have created a directory of templates called resources which contains a generic edit
and new
view.
I have multiple controllers that I would like to share these views (they have their own show templates and forms). Each of these controllers currently inherits from a base ResourceController
. By default, if any of these controllers is missing a view, Rails will follow the inheritance chain looking for a view, so if my NewsItems controller doesn't have an edit
template, Rails will automagically look for one in resources
. However, I would like to remove the need for all these controllers to inherit from a single controller. In my case this adds an extra controller into the inheritance chain which I don't want to do. I would prefer to move the resource introspection shared by the controllers into a mixin.
However, doing so will mean Rails no longer looks in the resources
view directory for missing templates.
I don't want to render the same 'resources/editand
resources/new` templates in every controller, so is there a way of declaring which template to use for which action in a declarative way? Preferably in the mixin?
It sounds like you may be looking for
prepend_view_path
. So with that you can have e.g. aMyResources
concern module you include in your controllers:Then in any controller that you include
MyResources
, the first path to check for view templates will beapp/views/resources/
, followed by the usual locations.