2013-10-02 177 views
0

我有這個在我的.html.erb代碼:路由錯誤在Ruby on Rails的

$.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 

而且在我的routes.rb:

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

但是當submited顯示:

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

問題在哪裏? 是在ajax網址或其他東西的路線?

+0

您有一個錯字:'match'/ tiempoMaquinaEdit「,':tiempoMaquinaEdit應該是'match'/ timeMachineEdit」 – MrYoshiji

回答

0

的問題是在你的路由定義....

嘗試匹配「/ timeMachineEdit」,到:「數據集#timeMachineEdit」

我認爲,它仍然不是因爲的工作性質format..In的datasets_controller試試下面的代碼...

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

而且改變你的AJAX請求爲「腳本」的數據類型,以便與其他format.js正確匹配它的格式將/這將會選擇您將在respond_to塊中指定的第一種格式。