1

I would like to send a javascript array to be processed by a method in my controller. I think I am doing this way wrong. I am a total RoR, jquery, and ajax noobie. Here is what I have. Please give me some guidelines:

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

I get this error:

POST http://10.10.136.244:6500/qtl_table/save 500 (Internal Server Error) 

and

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)

EDIT: app/controllers/qtl_table_controller.rb

...
  def save
    logger.debug "\nLOOK! WE SAVED! #{params[data]}\n"
    render "index"
  end
...

I added render :layout => false but I am still getting the missing template error. Also, someone suggested I just begin adding logic to my controller but the data parameter looks really funky. Its supposed to be an array of arrays that I turned into a string. Could you guys give me some more help?

4

3 回答 3

0

同意里亚切的回答。您需要向控制器添加一些逻辑才能正确响应。

查看控制台输出,您可以在参数哈希中看到 json 数据。您可以像这样简单地访问控制器中的值:

params["125,0,\"\",\"Ph upt 1-2\""]
params["125,0,\"\",\"Ph upt 1-2\""][","]

您可以将其映射到您的 activerecord 模型并保存。

于 2012-06-25T22:27:35.167 回答
0

您应该在控制器中创建模板app/views/qtl_table/save.html.erb或渲染某些内容。如果在控制器操作中没有呈现任何内容,Rails 会尝试显示默认模板,而您没有。

于 2012-06-25T22:22:54.287 回答
0

由于您希望它在 AJAX 中响应,您可以通过添加来告诉 Rails 不要渲染任何内容

render :layout => false

到控制器动作

于 2012-06-25T22:36:31.147 回答