2016-12-30 63 views
1

我一直在關注railscast 340教程,但是我一直無法通過js呈現數據。這個表對我來說是空的,數據不會顯示我,可能會做錯什麼?Ruby on Rails使用基於Rails Cast的數據表#340

這是我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 

我clienteController

def index 

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

我client.coffe

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

和我的表:

<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> 

在本地主機:3000/cliente.json我得到這個錯誤:

模塊:: DelegationError在ClientesController#指數

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:[email protected]/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">, 
+0

直接從瀏覽器中嘗試url,看看問題出在服務器端還是客戶端。像localhost:3000/cliente.json – sethi

+0

@sethi謝謝你的回答。我嘗試了localhost:3000/clientes.json,但我拋出了以下錯誤:未初始化常量ClientesController :: ClientesDatatable(在這一行:format.json {渲染json:ClientesDatatable.new(view_context)}) – LuisC

+0

這是什麼文件clientesdatatable.rb ??確保它的加載。 – sethi

回答