簡體   English   中英

Ruby on Rails在Ajax中的路由錯誤

[英]Routing error in ajax on ruby on rails

我的.html.erb代碼中有這個:

 $.ajax({
 url: "/timeMachineEdit",
 data: {editTimeMachine: newArray},
 type: 'POST',
 success: function (res) {
     if (res.result === 'ok') {
     alert('Data saved');
     } else {
     alert('Save error');
     }
},
error: function () {
     alert('Save error.');
     }
});

這在我的datasets_controller.rb中

def timeMachineEdit 
    @dataset = current_user.dataset
    @dataset.machine_time = params[:editTimeMachine]
end

在我的route.rb中:

match "/timeMachineEdit", to: "datasets#timeMachineEdit"

但是何時提交顯示:

POST http://localhost:3000/timeMachineEdit 500 (Internal Server Error) 

問題出在哪里? ajax url中的路由是什么?

問題出在您的路線定義中。

嘗試將“ / timeMachineEdit”匹配為:“ datasets#timeMachineEdit”

我認為,由於格式的性質,它仍然無法正常工作。在datasets_controller中嘗試以下代碼...

def timeMachineEdit 
  @dataset = current_user.dataset
  @dataset.machine_time = params[:editTimeMachine]
  respond_to do |format|
    format.js 
  end
end

還要將您的AJAX請求的dataType更改為“ script”,以使其與format.js正確匹配,否則格式將為/ ,這將選擇您在response_to塊中指定的第一種格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM