jbox-web / ajax-datatables-rails

A wrapper around DataTable's ajax methods that allow synchronization with server-side pagination in a Rails app
MIT License
590 stars 227 forks source link

Error message did not make it clear where the actual error occured #322

Closed shadydealer closed 3 years ago

shadydealer commented 5 years ago

Given we have the following code in our js file:

var table = $('#groups-datatable').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": $('#some_datatable').data('source'),
        "pagingType": "full_numbers",
        "columns": [
        {data: "some_column"}
        ]
  });

And the following in our some_model_datatable.rb file:

  def view_columns
    # Declare strings in this format: ModelName.column_name
    # or in aliased_join_table.column_name format

    @view_columns ||= {
      number_of_people: { source: "number_of_people"}
    }
  end

  def get_raw_records
    # insert query here
    Group.includes(:organization, :group_users).select("groups.*, (select count(*) from group_users where group_users.group_id = groups.id) as number_of_people")
  end

It displays the following error in the console:

NoMethodError - undefined method `fetch' for nil:NilClass:
  app/datatables/group_datatable.rb:25:in `data'
  app/controllers/groups_controller.rb:93:in `block (2 levels) in index'
  app/controllers/groups_controller.rb:92:in `index'

where in group_datatable.rb:25 there's the following code:

records.map do |record|
...

It would be nice if the error messages were a bit more descriptive, because we spent around 2-3 hours searching for the mistake, which was actually that the {data: "some_column"} value should be the same as the @view_columns key name (which is number_of_people).

n-rodriguez commented 3 years ago

Fixed : https://github.com/jbox-web/ajax-datatables-rails/commit/f778e5807bf6003bb3c397da22ea9e4993c7e48a