Javascript doesn't seem to get data from rails source

I am using dataTables to display a nice table to display various information. Javascript successfully sends database / webserver requests, however javascript doesn't seem to receive any current data.

Here is my controller

class CommentsController < ApplicationController
  before_filter :authenticate_user!
  before_filter :get_current_user

  # GET /comments
  # GET /comments.json

  def get_current_user
    @user=current_user
  end

  def index
    @comments = @user.comments

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @comments }
    end
  end

      

View:

<h1>Listing comments</h1>

<table id="comments_id" class="display">
<thead>
  <tr>
    <th>String</th>
    <th>secondString</th>
    <th></th>
    <th></th>
  </tr>
</thead>
<tbody>
</tbody>
</table>

<br />

<%= link_to 'New Comment', new_comment_path %>
<h1>Listing comments</h1>

<table id="comments_id" class="display">
<thead>
  <tr>
    <th>State</th>
    <th>Environment</th>
    <th>dns</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
</thead>
<tbody>
</tbody>
</table>

<br />

<%= link_to 'New Comment', new_comment_path %>
<h1>Listing comments</h1>

<table id="comments_id" class="display">
<thead>
  <tr>
    <th>State</th>
    <th>Environment</th>
    <th>dns</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
</thead>
<tbody>
</tbody>
</table>

<br />

<%= link_to 'New Comment', new_comment_path %>

      

comments.js:

function InitOverviewDataTable()
{
  oOverviewTable =$('#desktops_id').dataTable(
  {
    "bPaginate": true,
    "bJQueryUI": true,  // ThemeRoller-stöd
    "bLengthChange": false,
    "bFilter": false,
    "bSort": false,
    "bInfo": true,
    "bAutoWidth": true,
    "bProcessing": true,
    "iDisplayLength": 10,
    "sAjaxSource": 'desktops'
  });
}

function RefreshTable(tableId, urlData)
{
  $.getJSON(urlData, null, function( json )
  {
    table = $(tableId).dataTable();
    oSettings = table.fnSettings();

    table.fnClearTable(this);

    for (var i=0; i<json.aaData.length; i++)
    {
      table.oApi._fnAddData(oSettings, json.aaData[i]);
    }

    oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
    table.fnDraw();
  });
}

function AutoReload()
{
  RefreshTable('#desktops_id', 'desktops');

  setTimeout(function(){AutoReload();}, 3000);
}

$(document).ready(function () {
  InitOverviewDataTable();
  setTimeout(function(){AutoReload();}, 3000);
});

      

I can see the correct queries are being pushed on the database side:

Started GET "/comments" for 127.0.0.1 at 2013-01-22 18:46:16 -0500
Processing by CommentsController#index as JSON
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Comment Load (0.4ms)  SELECT "comments".* FROM "comments" WHERE "comments"."user_id" = 1
Completed 200 OK in 6ms (Views: 3.2ms | ActiveRecord: 0.9ms)

      

However on the firefox web console I can see that as the data is polled, no data is retrieved ... and when the dataTable gets stuck in "loading data"

[19:04:37.395] GET http://localhost:3000/comments?_=1358899476292 [HTTP/1.1 200 OK  38ms]
[19:04:39.344] GET http://localhost:3000/comments [HTTP/1.1 304 Not Modified  15ms]
[19:04:42.360] GET http://localhost:3000/comments [HTTP/1.1 304 Not Modified  18ms]

      

thanks for the help

+3


source to share


1 answer


The problem is, the JSON data coming from your server has no value for the key aaData

because you are serializing ActiveRecord

objects.

I would change the way I use the DataTable plugin altogether, because it looks like you are using internal APIs (for example _fnAddData

), which is a little annoying.



Instead, I would recommend the approach Ryan Bates shows in his RailsCast # 340 on Datatables. This method worked really well for me and I never had a problem with it.

0


source







All Articles