ajahongir / ajax-datatables-rails-v-0-4-0-how-to

ajax-datatables-rails v-0-4-0 sample project code
12 stars 6 forks source link

undefined method `map' for #<ActionController::Parameters #3

Open matissg opened 7 years ago

matissg commented 7 years ago

First of all ajax-datatables-rails is just awesome gem :) I implemented it on Rails 5.0.2. it's previous version, but since there is some params deprecation issue I'm trying to upgrade to Rails 5.1.0 and your v-0-4-0 version.

At the moment I'm a bit stuck as I get this error:

Started GET "/en/medias.json?draw=1&columns%5B0%5D%5Bdata%5D=name&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1493747406969" for 10.0.2.2 at 2017-05-02 17:49:56 +0000
Processing by Media::MediasController#index as JSON
  Parameters: {"draw"=>"1", "columns"=>{"0"=>{"data"=>"name", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}}, "order"=>{"0"=>{"column"=>"0", "dir"=>"asc"}}, "start"=>"0", "length"=>"10", "search"=>{"value"=>"", "regex"=>"false"}, "_"=>"1493747406969", "locale"=>"en"}
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
   (1.7ms)  SELECT COUNT(*) FROM "media_medias"
Completed 500 Internal Server Error in 123ms (ActiveRecord: 7.9ms)

NoMethodError (undefined method `map' for #<ActionController::Parameters:0x007f64980d2038>
Did you mean?  tap):

In Gemfile I have gem 'ajax-datatables-rails', github: 'ajahongir/ajax-datatables-rails', branch: 'v-0-4-0'

I have media_datatables.rb which looks like this:

class MediaDatatable < AjaxDatatablesRails::Base

  def view_columns
    @view_columns ||= {
      name: { source: "Media::Medias.name", cond: :like }
    }
  end

  private
  def data
    records.map do |media|
      {
        name: media.name
      }
    end
  end

  def get_raw_records
    Media::Medias.all
  end

end

Index action in controller looks like this:

class Media::MediasController < ApplicationController
  include Common::CompaniesHelper

  before_action :correct_media, only: [:edit, :update]

  def index
    respond_to do |format|
      format.html
      format.json do
        render json: MediaDatatable.new(view_context)
      end
    end
  end

index.html.erb is simple as this:

<table class="display responsive no-wrap" id="media-table", 
data-source="<%= media_medias_index_path(format: :json) %>">
  <thead>
    <tr>
      <th>Name</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

and my medias.coffee looks like this:

$ ->
  $('#media-table').dataTable
    processing: true
    serverSide: true
    responsive: true
    ajax: $('#media-table').data('source')
    pagingType: 'full_numbers'
    columns: [ { data: "name" } ]

I can see in my app it shows some elements of DataTables, so jQuery plugin is working. Can you take a look and say what am I missing, please?

ajahongir commented 7 years ago

well I have updated the gem, try to remove gem and install again. should work. where is fix commit - https://github.com/ajahongir/ajax-datatables-rails/commits/v-0-4-0