views:

448

answers:

3

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.

A: 

You could run script/generate scaffold test name:string description:text valid:boolean and look at the views that generates (and run script/destroy scaffold test to remove the files). That will give you a good sense of the standard way to write the 4 default Rails views.

I would also recommend reading the relevant chapters in "Agile Web Development with Rails" and "The Rails Way".

If you have existing views that need to be cleaned up, this episode of Railscasts is great: Cleaning Up the View

Dary

Dary
+1  A: 

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 read Editing <%= @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, Unlike redirect_to, render :action only 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.

Chirantan
A: 

Looks like there is just a new gem out that is very close to your requirements:

http://github.com/codez/dry_crud

Based on a common superclass (CrudController), you may inherit CRUD functionality for your various model controllers and adapt what is special there. This is what you already did, most probably.

The new thing about dry_crud is that also views and partials are inheritable. You define a common base template for each CRUD action, maybe divided into a couple of partials. Thanks to the provided helpers, forms and tables may be generically defined by looking at the column definitions of the current model. In your specific model's views, you then may adapt only the partials or views that need customizing.

Have a look at the documentation found on the site above and stay DRY!

pascal