# Weird POST/PUT behavior

**URL:** <https://www.ruby-forum.com/t/weird-post-put-behavior/181438>\
**Category:** Rails\
**Created:** [January 12, 2010, 1:26am UTC](https://www.ruby-forum.com/t/weird-post-put-behavior/181438 "2010-01-12T01:26:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Curtis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea5d25/32.png) [@Curtis\_C](https://www.ruby-forum.com/u/Curtis_C)\
**Post date:** [January 12, 2010, 1:26am UTC](https://www.ruby-forum.com/t/weird-post-put-behavior/181438/1 "2010-01-12T01:26:14Z")

</div>

I used the “Cleaning Up Controllers with Postback Actions” recipe from  
Rails Recipes. Basically you replace all the edit/new/update/create  
methods with one method that looks like:

def edit  
@recipe = Recipe.find\_by\_id(params[:id]) || Recipe.new  
if request.post?  
@recipe.attributes = params[:recipe]  
redirect\_to :main\_url and return if @recipe.save  
end  
end

My form uses form\_for with a url parameter:  
\<% form\_for(@schedule, :url =\> {:action =\> ‘edit’}) do |f| %\>

When I create a new entry and submit the form, it does a POST just  
like you’d expect, but when I edit an existing record and submit the  
form, the server claims it is doing a PUT. That means no updates since  
the record is only updated when the browser submits a POST action.  
Even the source in the browser claims the form is going to be  
submitted with a POST, but by the time it gets to the server, the  
server log claims it is a PUT. Any ideas? I’ve hacked around it for  
now by checking for POST or PUT but that feels, well, hacky.

Frankly I’m a little disappointed in the recipe. For it to work,  
you’ll need to change your form and possibly your routes.rb file, but  
those details are left out ☹

# – Curtis C. removed\_email\_address@domain.invalid home:[http://curtiscooley.com](http://curtiscooley.com) blog:[http://ponderingobjectorienteddesign.blogspot.com](http://ponderingobjectorienteddesign.blogspot.com)

Leadership is a potent combination of strategy and character. But if  
you must be without one, be without the strategy.  
– H. Norman Schwarzkopf

---

<div class="post-metadata">

**Author:** ![Curtis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea5d25/32.png) [@Curtis\_C](https://www.ruby-forum.com/u/Curtis_C)\
**Post date:** [January 12, 2010, 6:59am UTC](https://www.ruby-forum.com/t/weird-post-put-behavior/181438/2 "2010-01-12T06:59:18Z")

</div>

I haven’t looked at the recipe you refer to but most browsers will  
only do posts. That’s why Rails form helpers simulate doing a put by  
adding a hidden field called \_method to the form which rails then  
checks for before passing control onto your controller.

PUTs _are_ supposed to be used to update your records but the RESTful  
way of doing it is to put the update code in the update method and  
specifying resources in your routes.rb file.

Also using the REST convention, your edit method should respond to a  
GET and should display a form. It shouldn’t be updating anything.

This guide should set you on the right track:

> **[Getting Started with Rails — Ruby on Rails Guides](https://guides.rubyonrails.org/getting_started.html)**
>
> Getting Started with RailsThis guide covers getting up and running with Ruby on Rails.After reading this guide, you will know: How to install Rails, create a new Rails application, and connect your application to a database. The general layout of a...

Regards  
Ritchie

---

<div class="post-metadata">

**Author:** ![Curtis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea5d25/32.png) [@Curtis\_C](https://www.ruby-forum.com/u/Curtis_C)\
**Post date:** [January 12, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/weird-post-put-behavior/181438/3 "2010-01-12T18:00:06Z")

</div>

On Mon, Jan 11, 2010 at 9:57 PM, Ritchie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> GET and should display a form. It shouldn’t be updating anything.
> 
> This guide should set you on the right track: [Getting Started with Rails — Ruby on Rails Guides](http://guides.rubyonrails.org/getting_started.html)

Ok, so the recipe allows you to replace 4 similar methods with one  
method, which is more DRY, but from what Ritchie is saying, adhering  
to the RESTful interface is more important.

Is it ‘better’ to keep the four methods and DRY them up as best I can  
rather than trying the post back method recipe? I’m thinking so, since  
the post back recipe made me do some things I didn’t really like, like  
removing the resources entry from my routes. And it broke things.

Plus, I just discovered that the “Rails Recipes” book is for Rails  
1.0. Sheesh, I just bought it ☹

## Perhaps the post back model isn’t encouraged any more despite it’s DRYness.

# Curtis C. removed\_email\_address@domain.invalid home:[http://curtiscooley.com](http://curtiscooley.com) blog:[http://ponderingobjectorienteddesign.blogspot.com](http://ponderingobjectorienteddesign.blogspot.com)

Leadership is a potent combination of strategy and character. But if  
you must be without one, be without the strategy.  
– H. Norman Schwarzkopf

---

<div class="post-metadata">

**Author:** ![Curtis\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea5d25/32.png) [@Curtis\_C](https://www.ruby-forum.com/u/Curtis_C)\
**Post date:** [January 12, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/weird-post-put-behavior/181438/4 "2010-01-12T18:03:37Z")

</div>

Curtis C. wrote:

> On Mon, Jan 11, 2010 at 9:57 PM, Ritchie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > GET and should display a form. It shouldn’t be updating anything.
> > 
> > This guide should set you on the right track: [Getting Started with Rails — Ruby on Rails Guides](http://guides.rubyonrails.org/getting_started.html)
> 
> Ok, so the recipe allows you to replace 4 similar methods with one  
> method, which is more DRY, but from what Ritchie is saying, adhering  
> to the RESTful interface is more important.
> 
> Is it ‘better’ to keep the four methods and DRY them up as best I can  
> rather than trying the post back method recipe? I’m thinking so, since  
> the post back recipe made me do some things I didn’t really like, like  
> removing the resources entry from my routes. And it broke things.
> 
> Plus, I just discovered that the “Rails Recipes” book is for Rails  
> 1.0. Sheesh, I just bought it ☹
> 
> Perhaps the post back model isn’t encouraged any more despite it’s  
> DRYness.

Yeah, this looks like a mediocre idea. If you want to avoid repetition  
in your controllers, I’d suggest something like make\_resourceful.

> # – Curtis C. removed\_email\_address@domain.invalid home:[http://curtiscooley.com](http://curtiscooley.com) blog:[http://ponderingobjectorienteddesign.blogspot.com](http://ponderingobjectorienteddesign.blogspot.com)
> 
> Leadership is a potent combination of strategy and character. But if  
> you must be without one, be without the strategy.  
> – H. Norman Schwarzkopf

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
