1
railscast 340チュートリアルに従っていますが、js経由でデータをレンダリングできませんでした。テーブルは私には空で、データは私には表示されません、何が間違っている可能性がありますか?Railsベースのデータテーブルを使用したRuby Cast#340
これは私の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"
}
と私のテーブルの私の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
です:
<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>
ローカルホストで10
: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">,
ブラウザから直接URLを試して、問題がサーバー側かクライアント側かを確認してください。 localhost:3000/cliente.json – sethi
@sethi回答ありがとうございます。私はlocalhost:3000/clientes.jsonを試しましたが、次のエラーをスローします。初期化されていない定数ClientesController :: ClientesDatatable(この行:format.json {render json:ClientesDatatable.new(view_context)}) – LuisC
このファイルはclientesdatatable.rb ??それがロードされていることを確認してください。 – sethi