3

I am trying to submit a form, but if I just put form_for @classroom I get a "No route matches [POST]" error.

Now with the code posted below, I get the wrong url in the form. If I change the url manually in the browser it goes through, and I guess I could do that via javascript, but... why... is... this... happening..?

Until yesterday everything was working fine. I even tried rolling back to the things I changed but I can't seem to track what is going wrong.

routes.rb

patch 'classrooms/:id/update' => "classrooms#update", as: :update_classroom
resources :classrooms, except: :update

form from rails end

<%= form_for(update_classroom_path(@classroom), method: "patch") do |class_f| %>

form in browser

<form action="/classrooms/23/edit" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="******">

rake routes

        absences POST   /absences(.:format)                   absences#create
                 POST   /classrooms/:id/getAbsences(.:format) classrooms#getAbsences
update_classroom PATCH  /classrooms/:id/update(.:format)      classrooms#update
      classrooms GET    /classrooms(.:format)                 classrooms#index
                 POST   /classrooms(.:format)                 classrooms#create
   new_classroom GET    /classrooms/new(.:format)             classrooms#new
  edit_classroom GET    /classrooms/:id/edit(.:format)        classrooms#edit
       classroom GET    /classrooms/:id(.:format)             classrooms#show
                 DELETE /classrooms/:id(.:format)             classrooms#destroy
            root GET    /                                     pages#start
Community
  • 1
  • 1
Tashows
  • 545
  • 6
  • 21

4 Answers4

5

Just to answer your question from title, I think your form method is "PATCH" indeed. Refer to the guide http://guides.rubyonrails.org/form_helpers.html about how rails makes a patch form.

The Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PATCH" and "DELETE" requests (besides "GET" and "POST"). However, most browsers don't support methods other than "GET" and "POST" when it comes to submitting forms.

Rails works around this issue by emulating other methods over POST with a hidden input named "_method", which is set to reflect the desired method:

Community
  • 1
  • 1
andylee
  • 255
  • 2
  • 6
3

After some more trial and error, I realised I had left some plain input tags in a deeper nested level of the form (instead of going with the normal fields_for and separate builders for each level). I guess that somehow screwed up the relations and affected the method of the parent form.

That was such a mind blending mess up.

Edit: Andylee's answer is right. What I and Jeremy mention was probably the actual issue going on and not what was originally assumed to be the problem (as mentioned in the title).

Tashows
  • 545
  • 6
  • 21
3

To add a bit of specificity, to Tashow's answer above (which set me on the right track), I had some hidden fields that look'd like this, in a nested form.:

<%= hidden_field_tag("Classroom[classroom_teachers_attributes][]", nil) %>
<%= hidden_field_tag("Classroom[classroom_teachers_attributes][]", '') %>

Once I got rid of these, everything began working properly again. (There still remained similar-looking <input> tags generated by fields_for, etc.)

jeremy
  • 33
  • 1
  • 5
0

form_for takes an object as first argument, and its usually better to keep the REST-like way of rails handling the update method.

The action of you html form displays "/classrooms/23/edit" so yes it won't work.

form_for(@classroom, url: update_classroom_path(@classroom), method: "patch")
Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • I am aware of that. I would expect that only using @classroom, should automatically route to the patch route, but it doesn't seem to be the case. Your suggestion gives me 'No route matches [POST] /classrooms/23/update' as well :/ – Tashows Aug 12 '16 at 11:50
  • using only @classroom automatically route to the original patch route, but not your – Jaffa Aug 12 '16 at 13:25
  • It wasn't working with the original patch route either. That's why I made a custom route in the first place. I found out it was probably caused because of some hardcoded input fields I had in the form. You can read the answer bellow. Thanks for your input! – Tashows Aug 12 '16 at 14:42