I am writing an app which – similarly to many apps out there – is 90% regular CRUD things and 10% ‘juice’, where we need nasty business logic and more flexibility and customization.
Regarding this 90%, I was trying to stick to the DRY principle as much as I can. As long as controllers go, I have found resource_controller to really work, and I could get rid of all the controllers on that area, replacing them with a generic one.
Now I’d like to know how to get the same with the views. On this app I have an overall, application.html.erb layout and then I must have another layout layer, common for all CRUD views and finally a ‘core’ part:
-
On index.html.erb all I need to generate a simple table with the fields and labels I indicate.
-
For new and edit, also generic form edition, indicating labels and fields (with a possibility of providing custom fields if needed).
-
I am not sure I will need show, but if I do it would be the same as new and edit.
What plugins and tools (or even articles and general pointer) would help me to get that done?
Thanks, Felipe.
If you have DRYed up the controllers and now wish to DRY the views, one approach is to
render :action => *actionname*and storing the UI contents that may change into instance variables (So that they are available on the view) This way you would be able to re-use the same view of edit, new, list or show. For example, You are editing something related to Foo then you title should readEditing <%= @type %>, so should your form helpers. Foo could well then change to bar. Thus you are re-using the same view for different entities (or controllers should I say). Remember that, Unlikeredirect_to,render :actiononly renders the view and does not call the controller action of the action it is trying to render.One thing for sure, if you wish to DRY up anything, you need to standardize or follow a convention. Example, the structure of your views, in this case.