Since last few days, I am enjoying programing on RoR. But, yesterday, I became cracked for a stupid error. I wrote a route in rails 3 and it matched properly. But I was getting a error: “No route matches”. Opps, I guess you didn’t understand the error. Let me explain it briefly.
I created the route as following
match '/verify'=> 'products#verify', :as => :verify, :via => :post
And I wanted to use this route in the edit form. And, so, I set form action = “/verify” by verify_path and form method=”post”. But, unfortunately, I was getting error No route matches [PUT] “/verify”. Somehow, my post request becomes put.
So, I had to dig couple of hours to figure out the exact problem. It’s pretty interesting. Basically, rails follows the REST structure. But, not all browsers supports PUT and DELETE requests. Rails is a bit smart here. In order to handle this types of request rails use _method parameter. If you use form_for, it will add a hidden element as below though the form method is post.
<input name="_method" type="hidden" value="put" />
For delete link:
<a href="/products/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a>
happy hacking 🙂