Rails 3.1 simple_datatables
I have been looking at Simple Datatables but the do开发者_运维知识库cumentation and example provided are not very clear. It appears to implement jsonify and utilize a template handler but I am not sure. Also the provided code does not compile; is the author using some other gem to parse the views? For example, it says to put this in your search view...
@products.each do |product|
json << [product.name, product.manufacturer.name]
end
which I assuming has to be translated to...
<%= @products.each do |product| %>
<% json << [product.name, product.manufacturer.name] %>
<% end %>
but it comes back and says 'undefined local variable or method 'json' for class blah blah'
same type of deal for the index view. The example says to just throw this in your view,
%table#products
%thead
%tr
%th= Product.human_attribute_name :name
%th= Product.human_attribute_name :manufacturer
%tbody
but if I do that it simply puts that raw text in my view. Again, it seems like the author is using some gem to parse his views but not referencing it in the docs. Any thoughts? Thanks!
Edit:
Ok, so I got the table to load correctly by calling it as a table... duh...<table class="products">
<thead>
<tr>
<th>Name</th>
<th>Manufacturer</th>
</thead>
<tbody></tbody>
</table>
but it is now telling me it has no data to load. I checked the route and /search works but /search.datatables comes back telling me there is no template. Thanks again,
The author isn't using any other gem as far as I can tell.
@products.each do |product|
json << [product.name, product.manufacturer.name]
end
You need to put the above code into a search.datatables.jsonify file instead of just search.datatables.
Also you should change the class="products" to id="products" in your table markup. You will also want to put the corresponding id's into the th tags too.
Finally the javascript was a bit mangled too, try the following
$("#products").dataTable( {
"sAjaxSource" : "/products/search.datatables",
"aaSorting" : [[0, 'asc']],
"aoColumns" : [
{"sName":"name"},
{"sName":"manufacturer_name"},
],
"bServerSide" : true,
"fnServerData" : simpleDatatables
});
Hope this helps.
The index view is using the haml gem.
精彩评论