title: Rails 4 + datatables: Sort / search will not work
body: <p>I am trying to get jQuery's datatable to work for my website but I cannot get it to function on Rails 4.  When I try to sort the columns I get ther error: <code>jquery.dataTables.self-770bf6f….js?body=1:2525 Uncaught TypeError: Cannot set property 'data' of null</code></p>
  
  <p>Here is what I have so far:</p>
  
  <p><strong>products_controller.rb</strong></p>
  
  <pre><code>class ProductsController &lt; ApplicationController
    before_action :set_product, only: [:show, :edit, :update, :destroy]
  
    # GET /products
    # GET /products.json
    def index
          respond_to do |format|
              format.html
              format.json { render json: ProductsDatatable.new(view_context) }
          end
      end
  
    # GET /products/1
    # GET /products/1.json
    def show
    end
  
    # GET /products/new
    def new
      @product = Product.new
    end
  
    # GET /products/1/edit
    def edit
    end
  
    # POST /products
    # POST /products.json
    def create
      @product = Product.new(product_params)
  
      respond_to do |format|
        if @product.save
          format.html { redirect_to @product, notice: 'Product was successfully created.' }
          format.json { render :show, status: :created, location: @product }
        else
          format.html { render :new }
          format.json { render json: @product.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /products/1
    # PATCH/PUT /products/1.json
    def update
      respond_to do |format|
        if @product.update(product_params)
          format.html { redirect_to @product, notice: 'Product was successfully updated.' }
          format.json { render :show, status: :ok, location: @product }
        else
          format.html { render :edit }
          format.json { render json: @product.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /products/1
    # DELETE /products/1.json
    def destroy
      @product.destroy
      respond_to do |format|
        format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_product
        @product = Product.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def product_params
        params.require(:product).permit(:name, :category, :price)
      end
  end
  </code></pre>
  
  <p><strong>index.html.erb</strong></p>
  
  <pre><code>&lt;table id="products" data-source="&lt;%= products_url(format: "json") %&gt;"&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;Category&lt;/th&gt;
        &lt;th&gt;Price&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
  
    &lt;tbody&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>products.coffee</strong></p>
  
  <pre><code>jQuery -&gt;
      $('#products').DataTable
      "Processing": true
      "serverSide": true
      "Ajax": $('#products').data('source')
  </code></pre>
  
  <p><strong>product_datatable.rb</strong></p>
  
  <pre><code>class ProductsDatatable
    delegate :params, :link_to, :number_to_currency, to: :@view
  
    def initialize(view)
      @view = view
    end
  
    def as_json(options = {})
      {
        sEcho: params[:sEcho].to_i,
        iTotalRecords: Product.count,
        iTotalDisplayRecords: products.total_entries,
        aaData: data
      }
    end
  
  private
  
    def data
      products.map do |product|
        [
          product.name,
          product.category,
          product.price
        ]
      end
    end
  
    def products
      @products ||= fetch_products
    end
  
    def fetch_products
      products = Product.order("#{sort_column} #{sort_direction}")
      products = products.page(page).per_page(per_page)
      if params[:sSearch].present?
        products = products.where("name like :search or category like :search", search: "%#{params[:sSearch]}%")
      end
      products
    end
  
    def page
      params[:iDisplayStart].to_i/per_page + 1
    end
  
    def per_page
      params[:iDisplayLength].to_i &gt; 0 ? params[:iDisplayLength].to_i : 10
    end
  
    def sort_column
  #       columns = [Drug.name, Drug.category, Drug.price]
  #       byebug
          columns = %w[name category price]
      columns[params[:iSortCol_0].to_i]
    end
  
    def sort_direction
      params[:sSortDir_0] == "desc" ? "desc" : "asc"
    end
  end
  </code></pre>