Rails – you think it is a POST but really it is a PUT

May 17, 2011

Ruby on RailsI just came across an odd situation in Ruby on Rails where I was working on a fairly complicated form where sometimes you displayed and were editing an existing record, but sometimes you were displaying a blank form and creating a new one.

This all worked when I originally was just creating a new record each time but when I also allowed for users to update the data using the same form I hit a problem. Suddenly when I posted the form I received a Ruby on Rails “No Route Matches” error. Very odd!

I checked my routes file and it was definitely still there, I checked my rails log and it definitely was posting the data through. Eventually I spotted the problem and that was that rails wants to update using “Put” and create using “Post”. Because Put is not very well supported (if at all?) it uses a workaround to send a variable through, telling the rails app that the post request is really a put. It would be nice if this was reflected in the log that told me it was a Post – but I guess you can’t have everything.

Once you realise this the solution is simple, you allow your route to be accessed using either Post ir Put.