我想發送一個javascript數組,由我的控制器中的方法處理。 我覺得我這樣做是錯誤的。我是一個總RoR,jquery和ajax noobie。這是我的。請給我一些指導原則:Ruby on Rails:將JavaScript數據發送給ruby控制器
<div id="dataTable" class="dataTable" style="width: 680px;height: 300px; overflow: scroll"></div>
<button>Save!</button>
<script>
var first = true;
var totalChanges = new Array();
$("#dataTable").handsontable({
//...some code that generates appropriate array totalChanges
});
var data = //..some code
$("#dataTable").handsontable("loadData", data);
$(function() {
$("button").button();
$("button").click(function() {
alert("clicked");
$.ajax({
type: "POST",
url: "save",
data: JSON.stringify(totalChanges),
success: function() { alert("Success!"); }
});
});
});
</script>
我得到這個錯誤:
POST http://10.10.136.244:6500/qtl_table/save 500 (Internal Server Error)
和
Started POST "/qtl_table/save" for 10.64.229.59 at Mon Jun 25 16:58:46 -0500 2012
Processing by QtlTableController#save as
Parameters: {"125,0,\"\",\"Ph upt 1-2\""=>{","=>{"125,1,\"\",\"DOR364\""=>{","=>{"125,2,\"\",\"G19833\""=>nil}}}}}
LOGGER WORKS
Completed 500 Internal Server Error in 81ms
ActionView::MissingTemplate (Missing template qtl_table/save with {:formats=>[:html], :handlers=>[:rjs, :rhtml, :erb, :rxml, :builder], :locale=>[:en, :en]} in view paths "/usr/home/benjamin/phavubase/qtl/app/views", "/usr/home/benjamin/phavubase/qtl/ruby/1.8/gems/declarative_authorization-0.5.5/app/views", "/usr/home/benjamin/phavubase/qtl/ruby/1.8/gems/devise_cas_authenticatable-1.1.1/app/views", "/usr/home/benjamin/phavubase/qtl/ruby/1.8/gems/devise-1.2.1/app/views", "/usr/home/benjamin/phavubase/qtl/ruby/1.8/gems/kaminari-0.12.4/app/views"):
app/controllers/qtl_table_controller.rb:18:in `data'
app/controllers/qtl_table_controller.rb:25:in `save'
Rendered ruby/1.8/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.8ms)
編輯: 應用程序/控制器/ qtl_table_controller.rb
...
def save
logger.debug "\nLOOK! WE SAVED! #{params[data]}\n"
render "index"
end
...
我加了rende r:layout => false,但我仍然收到缺少的模板錯誤。另外,有人建議我剛開始添加邏輯到我的控制器,但數據參數看起來非常怪異。它應該是一個數組,我變成了一個字符串。你們能給我更多的幫助嗎?
你可以發佈你的控制器代碼來呈現什麼?有可能它可能正在保存,只是試圖在之後渲染模板。 – agmcleod
我目前在我的控制器中沒有任何邏輯。 – bdeonovic