Question: Ruby on Rails using Data Table based on Rails Cast #340

Question

Ruby on Rails using Data Table based on Rails Cast #340

Answers 1
Added at 2016-12-31 00:12
Tags
Question

I have been following the railscast 340 tutorial, but I have not been able to render the data via js. The table is empty to me and the data does not show me, what could be doing wrong?

this is my clientesdatatable.rb

class ClientesDatatable
delegate :params, :h, :link_to, :number_to_currency, to: :@views

def initialize(view)
  @view = view
end

def as_json(options = {})
  {
    sEcho: params[:sEcho].to_i,
    iTotalRecords: Cliente.count,
    iTotalDisplayRecords: Clientes.total_entries,
    aaData: data
  }
end
def data
  clientes.map do |cliente|
    [
      h(cliente.IdCli),
      h(cliente.IdCli),
      h(cliente.IdCli),
      h(cliente.IdCli),
      h(cliente.IdCli),
      h(cliente.IdCli),
      h(cliente.IdCli),
      h(cliente.IdCli),
      h(cliente.IdCli),
      h(cliente.IdCli),
      h(cliente.IdCli),
      h(cliente.IdCli)
    ]
  end
end

def clientes
  @clientes ||= fetch_clientes
end

def fetch_clientes
  clientes = Cliente.order("#{sort_column} #{sort_direction}")
  clientes = clientes.page(page).per_page(per_page)
  if params[:sSearch].present?
    clientes = clientes.where("IdCli like :search", search: "%#{params[:sSearch]}%"))
end
clientes
end



def page
  [:iDisplayStart].to_i/per_page + 1
end

def per_page
  params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10

end



def sort_column
    columns = %w[IdCli IdCli IdCli IdCli IdCli IdCli IdCli IdCli IdCli IdCli IdCli IdCli IdCli]
#   columns[params[:iSortCol_0].to_i] <-- used in video
    columns[params[:order]["0"]["column"].to_i] #<-- works in Rails 4
end

def sort_direction
    params[:order]["0"]["dir"] #<-- works in Rails 4
#   params[:sSortDir_0] == "desc" ? "desc" : "asc" <-- used in video
end


end

my clienteController

  def index

  respond_to do |format|
    format.html
    format.json { render json: ClientesDatatable.new(view_context)}
  end

my client.coffe

jQuery ->
  $('#clientes').DataTable
    "Processing": true
    "ServerSide": true
    "Ajax": {
      url: $('#clientes').data('source'),
      type: "get"
    }

and my table:

<table id="clientes" class="display" data-source="<%=clientes_url(format: "json")%>"><!--el id clientes es de datatables referenciado en clientes.coffe y display class es una clase de datatables-->
  <thead>

    <tr><!--active es para sombrear la fila-->
      <th>Clave</th>
      <th>Nombre</th>
      <th>Nombre Corto</th>
      <th>Dirección</th>
      <th>Colonia</th>
      <th>Credito</th>
      <th>DiasCredito</th>
      <th>LimiteCredito</th>
      <th>Saldo</th>
      <th>Ruta</th>
      <th>Promociones</th>
      <th>Acción</th>
      <th></th>

    </tr>
  </thead>
    <tbody id="container_clientes">
</tbody>


</table>

in localhost:3000/cliente.json I get this error:

Module::DelegationError in ClientesController#index

ClientesDatatable#params delegated to @views.params, but @views is nil: #<ClientesDatatable:0x84da9678 @view=#<#<Class:0x84dc0b20>:0x84db4528 @_routes=nil, @_config={}, @view_renderer=#<ActionView::Renderer:0x84db45dc @lookup_context=#<ActionView::LookupContext:0x84f4b4f4 @details_key=nil, @details={:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :haml]}, @skip_default_locale=false, @cache=true, @prefixes=["clientes", "application"], @rendered_format=:json, @view_paths=#<ActionView::PathSet:0x84f4b490 @paths=[#<ActionView::OptimizedFileSystemResolver:0x85542bb4 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @cache=#<ActionView::Resolver::Cache:0x85542ba0 @data=#<ActionView::Resolver::Cache::SmallCache:0x85542b8c @backend={}, @default_proc=#<Proc:0x8275fa30@/home/luis/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionview-4.2.6/lib/action_view/template/resolver.rb:49 (lambda)>>>, @path="/home/luis/sites/AdvanceControldatatablejs2017/app/views">,
Answers
nr: #1 dodano: 2017-01-01 23:01

I have found the solution thanks to the instructions made by this gem https://github.com/antillas21/ajax-datatables-rails/wiki/Part-1----The-Installation

Source Show
◀ Wstecz