- To mitigate the first change (Datatables no longer inherits from
AjaxDatatablesRails::Base
but fromAjaxDatatablesRails::ActiveRecord
)
Create a new ApplicationDatatable
class and make all your classes inherits from it :
class ApplicationDatatable < AjaxDatatablesRails::ActiveRecord
end
class PostDatatable < ApplicationDatatable
end
Note : This is now in the ProTips™ section of the documentation.
- To mitigate the second change (The
view_context
is no longer injected in Datatables)
Update the ApplicationDatatable
class :
class ApplicationDatatable < AjaxDatatablesRails::ActiveRecord
extend Forwardable
attr_reader :view
def initialize(params, opts = {})
@view = opts[:view_context]
super
end
end
and update your controllers :
# before
respond_to do |format|
format.json { render json: UserDatatable.new(view_context) }
end
# after
respond_to do |format|
format.json { render json: UserDatatable.new(params, view_context: view_context) }
end
# if you need to inject some options
respond_to do |format|
format.json { render json: UserDatatable.new(params, view_context: view_context, my: 'options') }
end
This way, you can still use def_delegators
in your datatables as in the documentation.
Note that the recommanded way is to use Draper gem to separate filtering logic from view/presentation logic as in the documentation.