2015-12-12 20 views
1

所以,我有這種形式的聲明:的Rails:方法=>:補丁不​​起作用

<%= form_for 'students_list', {:url => update_students_list_stream_url(@stream), :method=>:patch} do |students_list_form| %> 
正如API文檔中描述

,但這使我的錯誤:

No route matches [POST] "/streams/26/edit-students-list" 

因此,它仍然會嘗試後,即使我的HTML輸入有:

<input type="hidden" name="_method" value="patch" /> 

從Rails的指南:

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:

我很困惑

+0

如果我找到你的話,你正在嘗試更新學生列表流,這是正確的嗎? –

+0

Rails使用['Rack :: MethodOverride'中間件](https://github.com/rack/rack/blob/857641dab255dbec490fead1d3a0f1ff999b2137/lib/rack/method_override.rb)處理請求,然後將其傳遞到路由。 'Rack :: MethodOverride'應該在'_method'參數中選擇,我對它爲什麼不工作也有點困惑。我建議嘗試發送帶有cURL或Postman的請求來排除故障,因爲您正在消除與瀏覽器相關的任何錯誤或與該表單相關的任何內容。 – max

+0

請注意,Rails unobtrustive JavaScript驅動程序也會發送本地PATCH/PUT/DELETE請求的一些技巧,這也可能是問題的一部分。 – max

回答

1

你會得到更好的做這樣的:

<%= form_for @stream do |student_form_list| %> 

如果你設置了使用標準resources directive你的路線,你將有以下路線:

enter image description here

這些路線中,update路徑應該是只是students_list_stream_path - 不是您現在有update_students_list_stream_path

如果您設置了form_for以使用正確的對象,它將自動設置路徑&方法update

+2

我發現,由於某種原因,方法聲明不起作用(除了post和get),如果表單構建器有任何隱藏字段。當我刪除'students_list_form.hidden_​​field'時,它開始工作併發送PATCH請求。不知道這是否是一些Rails錯誤,或者如果我需要隱藏字段,該怎麼辦。 –

+0

你有什麼隱藏的輸入代碼? –