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?

bdeonovic
  • 4,130
  • 7
  • 40
  • 70

3 Answers3

0

You should either create a template in app/views/qtl_table/save.html.erb or render something in your controller. If nothing is rendered in the controller action, Rails tries to display default template, and you don't have one.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • I have render "index" in my controller (thats the page that the app is currently on). I want the page to be reloaded after the save button is clicked – bdeonovic Jun 26 '12 at 13:59
0

Agreed with Riateche's answer. You'll need to add some logic to the controller to respond correctly.

Looking at your console output, you can see the json data in the parameters hash. You can simply access the values in your controller as such:

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

You can just map it your activerecord model and save it.

agmcleod
  • 13,321
  • 13
  • 57
  • 96
0

Since you want it to respond in AJAX, you can tell Rails not to render anything by adding

render :layout => false

to the controller action

Ron
  • 1,166
  • 5
  • 15