2016-09-19 67 views
0

分開後,我有一個導軌對象的字符串,例如:Rails的轉換模型與逗號

Book {id:1, name: "name1"} 
Book {id:2, name: "name2"} 

我使用DataTables和最好的ajax數據是得到對象作爲一個字符串數組,沒有列名。

我怎樣才能改變Book.all數據的陣列,而不列名

預期的結果是這樣的

感謝

{ 
    "data": [ 
    [ 
     "Tiger Nixon", 
     "System Architect", 
     "Edinburgh", 
     "5421", 
     "2011/04/25", 
     "$320,800" 
    ], 
    [ 
     "Garrett Winters", 
     "Accountant", 
     "Tokyo", 
     "8422", 
     "2011/07/25", 
     "$170,750" 
    ], 
    [ 
     "Ashton Cox", 
     "Junior Technical Author", 
     "San Francisco", 
     "1562", 
     "2009/01/12", 
     "$86,000" 
    ] 
] 
} 

回答

0

使用拔毛方法像下面,那麼你就可以得到陣列值。

Book.pluck(:title, :author) 
0

在Rails中實現DataTables。

使用這種寶石jquery-datatables-railsClick Here

你必須從控制器獲取的數據作爲軌道對象。並在視圖中填充表格。最後觸發數據表功能表上的這樣

`

$('#yourTable').DataTable({ 
    // ajax: ..., 
    // autoWidth: false, 
    // pagingType: 'full_numbers', 
    // processing: true, 
    // serverSide: true, 

    // Optional, if you want full pagination controls. 
    // Check dataTables documentation to learn more about available options. 
    // http://datatables.net/reference/option/pagingType 
}); 

無需轉換成字符串或JSON