我想发送一个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