所以這就是我想要做的。使用AJAX,我做一個PUT通話,那麼它應該促使相應的JavaScriptPUT AJAX路由
def update
@todo = Todo.find(params[:id])
if @todo.update_attribute(:done, true)
format.js
format.html {redirect_to todos_path, :notice => "Your todo item was marked done!"}
else
redirect_to todos_path, :notice => "Couldn't update your task"
end
end
而這裏的相關路線:
PUT /todos/:id(.:format) todos#update
而這裏的AJAX調用我從一個事件處理程序作出:
var _id = $(this).attr('id');
//Actual AJAX call
$.ajax({
type: "PUT",
url: "/todos/"+_id,
});
但是,當執行AJAX調用時,我的update.js.erb
中的任何內容都不會執行。
update.js.erb
<% if @todo.valid? %>
console.log("in here");
$(".todones").prepend('<li>hello</li>');
<% else %>
console.log("or here");
<% end %>
我也得到一個:error 500 internal server error
,下面服務器日誌:
Started PUT "/todos/264?_method=PUT" for 127.0.0.1 at 2014-03-18 14:25:11 -0400
Processing by TodosController#update as */*
Parameters: {"id"=>"264"}
[1m[36mTodo Load (11.8ms)[0m [1mSELECT "todos".* FROM "todos" WHERE "todos"."id" = ? LIMIT 1[0m [["id", "264"]]
[1m[35m (0.1ms)[0m begin transaction
[1m[36mSQL (0.4ms)[0m [1mUPDATE "todos" SET "done" = ?, "updated_at" = ? WHERE "todos"."id" = 264[0m [["done", true], ["updated_at", Tue, 18 Mar 2014 18:25:11 UTC +00:00]]
[1m[35m (1.0ms)[0m commit transaction
Completed 500 Internal Server Error in 17ms
ArgumentError (too few arguments):
app/controllers/todos_controller.rb:34:in `format'
app/controllers/todos_controller.rb:34:in `update'
什麼錯 - 以及如何解決它?
你可以從你的錯誤發佈堆棧跟蹤?它應該在服務器日誌log/development.log中。另外,做type:'PUT'可能不起作用,你可能不得不使用Rails在查詢字符串中傳遞_method = PUT的解決方法:url:「/ todos /」+ _id +「?_ method = PUT」 – DiegoSalazar
@diego。 greyrobot我試過你的後一個建議,那沒用。儘管這裏是服務器日誌! http://ideone.com/zwPDFf – Louis93