Sharing view between new and edit actions

Hi,

I had an argument with the web designer about a philosophical(not very practical) issue.

Normally, we have new.html.erb and edit.html.erb and they share _form.html.erb as a partial render. The designer said that there should be only one view template and use if-statement for minor differences.

For example, they all should be combined into form.html.erb. She thinks that it's better design-wise. No redundancy, blah blah...

I am against it. But I failed to convince her.

What do you think?

Thanks.

Sam

I wrote my app in your way. So I support you. I don't think it is better to write only one template for both new and edit. Because they are different methods in controller, and sometimes they take different parameters and submit different parameters too.

boblu is correct. Look at it from a RESTful perspective: what action is being executed? Create and update are separate actions, thus they need to be represented by separate files. Any crossover and redundancy is to be resolved through the inclusion of partials.

The web designer is wrong in thinking that because you have separate edit and new views files that most of the page is different and not sharing a greate deal of code. They are ignoring the fact that most template view are views within views. Especially if you are using a partial - where you have a template within a template within a template.

The outer template is the layout. Into this is inserted the view. And into this is inserted the form partial. In effect you have:

layout | view | partial | view | layout

Where the partial is within the view which is within the layout. A classic onion skin type arrangement. With your two views what you effectively have are:

layout | edit | form | edit | layout

and

layout | new | form | new | layout

So by choosing a different view you are doing one big if. That is if view is 'edit' slot the 'edit' template in the middle layer, or if the view is 'new' slot the 'new' template into the middle layer.

What's more this is far neater than using lots of if statements with a single template. You've separated the parts that need to be different into two separate files, so far less chance that you'll confuse the two actions : much less likely that something that should only occur in a new action will pop up in an edit action.

You've also reduced the number of times you need to run the if. Just once - to decide which middle template to use, rather than at every point in the single template where a choice needs to be made.

I think your developer is demonstrating a fundamental lack of understanding about how Rails templates work.