# Scaffold: What's the advantage of actions create/update?

**URL:** <https://www.ruby-forum.com/t/scaffold-whats-the-advantage-of-actions-create-update/120885>\
**Category:** Rails\
**Created:** [November 21, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/scaffold-whats-the-advantage-of-actions-create-update/120885 "2007-11-21T13:33:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 21, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/scaffold-whats-the-advantage-of-actions-create-update/120885/1 "2007-11-21T13:33:34Z")

</div>

Hi all

I’m quite unsure what’s the point to have a create and update action in  
the Rails scaffolds… I guess the developers had good reason to  
implement them, but I don’t really see the reason for them…

Why no just checking for get/post within the same new/edit method?

def new  
unless request.post?  
# do the normal “new” stuff  
else  
# do the stuff from the “create” method  
end  
end

def edit  
unless request.post?  
# do the normal “edit” stuff  
else  
# do the stuff from the “update” method  
end  
end

Anybody can tell me what’s the big advantage of splitting the above  
methods into new/create and edit/update methods?

Thanks a lot,  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 21, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/scaffold-whats-the-advantage-of-actions-create-update/120885/2 "2007-11-21T16:02:17Z")

</div>

Because the code is much more readable when it’s separated out into  
different methods and allow routing to direct the request to the  
appropriate method based on post/get.

The way you described above would work but the way the scaffold  
generator is creating is it to have each method be as simple as  
possible. I don’t see an advantage to putting them into one method,  
any code you want them to share can be re-factored out into a before  
filter which runs before each method

On Nov 21, 6:33 am, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 24, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/scaffold-whats-the-advantage-of-actions-create-update/120885/3 "2008-03-24T21:50:56Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Because the code is much more readable when it’s separated out into  
> different methods and allow routing to direct the request to the  
> appropriate method based on post/get.
> 
> The way you described above would work but the way the scaffold  
> generator is creating is it to have each method be as simple as  
> possible. I don’t see an advantage to putting them into one method,  
> any code you want them to share can be re-factored out into a before  
> filter which runs before each method
> 
> On Nov 21, 6:33 am, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

But how to “DRY” with the views? Both views (edit.html.erb and  
new.html.erb) seem to contain similar code.  
Shall I use something like a \_form.html.erb ?  
Or shall I render only one view in the controller?

TIA,  
Martin

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 24, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/scaffold-whats-the-advantage-of-actions-create-update/120885/4 "2008-03-24T21:54:49Z")

</div>

Oh, and yeah, you should use a \_form.rhtml.erb as long as the forms are  
the same ones. 🙂

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 24, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/scaffold-whats-the-advantage-of-actions-create-update/120885/5 "2008-03-24T21:54:09Z")

</div>

> But how to “DRY” with the views? Both views (edit.html.erb and  
> new.html.erb) seem to contain similar code.  
> Shall I use something like a \_form.html.erb ?  
> Or shall I render only one view in the controller?
> 
> TIA,  
> Martin

They seem as long as it’s only basic functionality. But the more complex  
a project becomes, the more the new and edit views will differ, so don’t  
“over-DRY” a project before it represents all your needs. Elsewise you  
may come to a point where you “un-DRY” code you have wasted hours to  
“DRY” before.
