Topic: RailsCast#340 - JSON not well formed for dataTables

In the RailsCast#340 Datatables, it appears that the JSON array returned is not what is expected by DataTables.

Let say for a model User where mapping returns, user.last_name, user.first_name, the returned JSON is

aaData: [[1, DOE, John,,…],[7, BAXTER, Bill,],[15, BROWN, Bob,],...]

wich is a one dimension array, and the 2 dimensions arraay expected by DataTables (for the aaData part) should be for the same set of data

aaData: [{"id":1, "last_name":"DOE", "first_name":"John", "email":"",…},{"id":7, "last_name":"BAXTER", "first_name":"Bill", "email":""],{"id":15, "last_name":"BROWN", "first_name":"Bob", "email":""},...]

So if it displays well in the example provided it throws error when filtering server-side with an error

"JSON data from server could not be parsed. This is caused by a JSON formatting error"

Is there anybody who knows how to return the well formatted JSON ?

Any help would be appreciate

Last edited by anso (2013-03-02 14:55:48)

Re: RailsCast#340 - JSON not well formed for dataTables

Can you post the JS where you are initializing Datatables?

Re: RailsCast#340 - JSON not well formed for dataTables

Hi musicman,
thanks for answering...

I solved the 'JSON' format problem, it was not due to my js but to the way I did my objects mapping ... I made my original mapping following the RailsCast, but it is made for an older version of dataTables, now I map my objects with

and the JSON is conform to the one expected by dataTables

Now the problem I'm facing is due to the fact I use CoffeeScript and have a serious lack of knowledge with it, for example I cannot initialize 'aoColums' correctly, (the columns are read as only one block...)-- You can follow the discussion wuth Allan on … ons-needed
I guess I will try to use only 'pure jQuery, let the CoffeeScript part for later...

The next challenge that I could hit is to have it to work with joined tables... I had it working with the 'bad' JSON, hope it will work the same with the good one...