I have a nested polymorphic resource, question, which may be nested beneath an exam. In the UI I build a form dynamically, using POST as the submit method. This works fine for the new action, POSTing to /exams/1/questions.
The issue is that for the edit action I get a Routing error (No route matched [POST]). I have checked the routes and can see that this is a valid error- there is a route for PUT which is connected to the update method.
My question is why does the edit action of a scaffolded model which uses POST as the form’s method work correctly, while my form submission generates the routing error above.
Here are the routes from my routes.rb:
resources :exams do
resources :questions
end
Here is the dynamically generated form
<form method="POST" action="/exams/1/questions/9">
<input name="authenticity_token" type="hidden" value="XXYYZZ=">
<input name="[question]template" value="Image Answers"><input name="[question]text" value="Which of these is a duck?">
</form>
I am aware that I can add post :create to the members of questions to map this manually, but I’d like to know why this doesn’t work out of the box.
A quick test application, shows that a standard form_for includes a method field where the put is specified. Adding this field to my dynamically generated form solved the issue.