# Sharing view between new and edit actions

**URL:** <https://www.ruby-forum.com/t/sharing-view-between-new-and-edit-actions/153176>\
**Category:** Rails\
**Created:** [November 12, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/sharing-view-between-new-and-edit-actions/153176 "2008-11-12T23:33:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [November 12, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/sharing-view-between-new-and-edit-actions/153176/1 "2008-11-12T23:33:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [November 13, 2008, 3:24am UTC](https://www.ruby-forum.com/t/sharing-view-between-new-and-edit-actions/153176/2 "2008-11-13T03:24:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [November 13, 2008, 9:48am UTC](https://www.ruby-forum.com/t/sharing-view-between-new-and-edit-actions/153176/3 "2008-11-13T09:48:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [November 13, 2008, 11:39am UTC](https://www.ruby-forum.com/t/sharing-view-between-new-and-edit-actions/153176/4 "2008-11-13T11:39:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [November 13, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/sharing-view-between-new-and-edit-actions/153176/5 "2008-11-13T21:46:18Z")

</div>

> She thinks that it’s better design-wise.  
> No redundancy, blah blah…

 She's a Web Designer... she should go design her CSS files and make pretty pictures with Photoshop, and leave the implementation details to those who know about implementation details...

the designer shouldn’t care if its Rails, or php, or the “Next Big  
Thing”…

after all you don’t pee on her images… (you don’t do you? Because that  
would be bad form)
