我有一個rails應用程序,每次單擊某個輸入字段時都會持續保存。你從/ posts/new開始。一旦你寫了一些東西然後點擊輸入,表單就會通過ajax提交。/posts/new的表單屬性然後更改爲/ post/1/edit的表單屬性(1可以是任意數字)。並且,網址更新爲/ post/1/edit(再次,1可以是任意數字)。rails ajax表單動作重寫路由錯誤
當表單被再次提交,與/後/ 1 /編輯和更新表單屬性,我得到以下錯誤:
No route matches [POST] "/posts/4"
谷歌瀏覽器的控制檯,我得到以下錯誤:
POST http://0.0.0.0:3000/posts/1 404 (Not Found)
這是我的代碼。
/app/asset/javascripts/posts.js
$(document).ready(function() {
$('.new_post').on('focusout', function(ev) {
$('.new_post').submit();
});
$('.edit_post').on('focusout', function(ev) {
$('.edit_post').submit();
});
});
/app/controllers/post_controller.rb,/app/models/post.rb和/config/routes.rb是控制器,型號和從腳手架產生的路線。
應用程序/視圖/職位/ _form.html.erb
<%= form_for(@post, :remote => true) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
而且,多了一些東西。 1)如果我只是去/後/ 1 /編輯,做一個編輯,並點擊輸入字段;沒有路由錯誤。
2)在表單動作的posts/new和post/1/edit頁面生成的html的ajax更新後生成的html是相同的。
任何幫助將不勝感激。
謝謝。
我以爲我試過這個解決方案,但我必須實現它不正確,因爲改變方法來「放」它的工作。 謝謝! – user2517182