0

I am trying to implement rails server side datatable with ajax-datatables-rails gem. When I want to see table, I get an error undefined method search. This is my code

 class ContractorDatatable < AjaxDatatablesRails::Base

  def view_columns
    # Declare strings in this format: ModelName.column_name
    # or in aliased_join_table.column_name format
    @view_columns ||= {
       id: { source: "Contractor.id", cond: :eq },
       name: { source: "Contractor.name" },
       city: { source: "Contractor.city" },
       ico: { source: "Contractor.ico" },
       country: { source: "Contractor.country" }
    }
  end
  def data
    records.map do |contractor|
      {
        # example:
         id: contractor.id,
         name: contractor.name,
         city: contractor.city,
         ico: contractor.ico,
         country: contractor.country
        }
    end
  end
  private
def contractors
  @contractors ||= fetch_records
end
def get_raw_records
  Contractor.all
end
def get_raw_record_count
  search_records(get_raw_records).count
end

end

In my controller I put it into index

class ContractorsController < ApplicationController
  before_action :authenticate_user!, :except => [:showcontractor,:listcontractors,:index]
  before_action :set_contractor, only: [:show, :edit, :update, :destroy]
  respond_to :html, :json
  # GET /contractors
  # GET /contractors.json
  def index
    @contractors = Contractor.all
    respond_to do |format|
      format.html
      format.json do
        render json: ::ContractorDatatable.new(view_context)
      end
    end
  end

and javascript is simple calling this json from ajax. But in console I see that get request for JSON returns 0 records and undefined method search error.

NoMethodError (undefined method `search' for "Contractor.name":String):

I have experience with datatables and server side in PHP, but I am kinda lost how it works in rails. thx

edit: javascript code

var tableconready = function() {
    $('#admindodavatelia').DataTable({
      'order': [0, 'asc'],
      'serverSide': true,
      'ajax' : '/contractors.json',
      'language': {
       'url': '/datatables_slovak.json'
      }
    });
    };
  $(".contractors.index").ready(tableconready);
  $(".contractors.index").on('turbolinks:load', tableconready); 

edit2: I cleaned the ContractorDatatable code to have it like in tutorial, but I get

(1.4ms) SELECT COUNT() FROM "contractors" (2.0ms) SELECT COUNT() FROM (SELECT "contractors".* FROM "contractors") AS foo Completed 500 Internal Server Error in 8ms (ActiveRecord: 3.5ms)

NoMethodError (undefined method []' for nil:NilClass):
app/datatables/contractor_datatable.rb:20:in
data'
app/controllers/contractors_controller.rb:13:in block (2 levels) in index' app/controllers/contractors_controller.rb:10:inindex'

7
  • and your javascript code? Who do you request the datatable? Commented Nov 8, 2016 at 11:57
  • added javascript code..what do you mean by who do you request? Commented Nov 8, 2016 at 20:40
  • Who generate the url you request the datatable Commented Nov 8, 2016 at 20:45
  • what tutorial you follow? (I do not understand some lines in your AjaxDatatable file, like model_name = .. columns = , searchable_columns = , super...) Commented Nov 8, 2016 at 20:47
  • which branch are you using of ajax-datatables-rails? Commented Nov 8, 2016 at 20:56

3 Answers 3

2

Try with this

var tableconready = function() {
    $('#admindodavatelia').DataTable({
      'order': [0, 'asc'],
      'serverSide': true,
      'ajax' : '/contractors.json',
      'language': {
       'url': '/datatables_slovak.json'
      },
      columns: [
        {data: 'id' },
        {data: 'name' },
        {data: 'city' },
        {data: 'ico' },
        {data: 'country' },
      ]

    });
  };
  $(".contractors.index").ready(tableconready);
  $(".contractors.index").on('turbolinks:load', tableconready); 

You miss the definition of columns

And in the AjaxDatatable the get_raw_record_count and contractors functions you do not need it.

Sign up to request clarification or add additional context in comments.

Comments

1

I just check that this is a gem version issue of ajax-datatables-rails. I got the same issue when I am using version 0.4.0 but when switching back to 0.3.1 this issue goes out.

Comments

0

Well, there are 2 types of binding datatable.net and datatable.rb columns:

  1. by default datatable.net pass columns by index.

    class CityDatatable < AjaxDatatablesRails::Base
    
      def view_columns
    
        @view_columns ||= {
          '0' => { source: "City.id", cond: :eq },
          '1' => { source: "City.name" },
          ...
        }
    
      end
    end
    
    
    $('#city').DataTable({
      'serverSide': true,
      'ajax' : '/cities.json'
    });
    
  2. binding by column name.

    class CityDatatable < AjaxDatatablesRails::Base
    
      def view_columns
        @view_columns ||= {
          **id**: { source: "City.id", cond: :eq },
          **name**: { source: "City.name" },
          ...
        }
      end
    end
    
    
    $('#city').DataTable({
      'serverSide': true,
      'ajax' : '/contractors.json'
    },
    columns: [
      {data: '**id**' },
      {data: '**name**' },
      ...
    ]
    });
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.