Ruby on rails Rails3数据表JSON-aData未定义
我知道这个问题以前被问过很多次,但是由于我缺乏经验,我无法用这些回答来纠正这个问题。我让它在以前的Rails项目中工作,但看不到发生了什么变化。我使用PostgreSQL而不是MySQL启动了一个新项目,现在我使用的是Rails 3.2.8(WAS3.2.1) 萤火虫显示: TypeError:aData在以下位置未定义:Ruby on rails Rails3数据表JSON-aData未定义,ruby-on-rails,json,datatables,Ruby On Rails,Json,Datatables,我知道这个问题以前被问过很多次,但是由于我缺乏经验,我无法用这些回答来纠正这个问题。我让它在以前的Rails项目中工作,但看不到发生了什么变化。我使用PostgreSQL而不是MySQL启动了一个新项目,现在我使用的是Rails 3.2.8(WAS3.2.1) 萤火虫显示: TypeError:aData在以下位置未定义: var aData = _fnGetObjectDataFn( oSettings.sAjaxDataProp )( json ); for ( var i=0, iLen=
var aData = _fnGetObjectDataFn( oSettings.sAjaxDataProp )( json );
for ( var i=0, iLen=aData.length ; i<iLen ; i++ )
我看到它不包括iTotalRecords、iTotalDisplayRecords和aaData。这就是问题所在吗?我假设JSON选项是由def设置为_JSON的,但是我没有在渲染中明确引用它??。http get(如Firebug中所示)包括sEcho,但不包括iTotalRecords
我的代码基于RailsCasts#340:
控制器:(参见列表操作)
和列表视图:
<% @page_title = "Highways List" %>
<h2>Highways</h2>
<%= javascript_tag do %>
window.highwaysURL = '<%= j highways_url %>';
<% end %>
<div id="EditLink">
<%= link_to "Edit Highway", :controller => :highways, :action => :edit, :id => 1 %>
</div>
<div>
</br>
<%= link_to 'Neighbours', '/neighbours/' %>
<%= link_to 'Localities', '/localities/' %>
</br>
</div>
<div id="tools">
</div>
<table id="highways" class="display" data-source="<%= highways_url(format: "json") %>">
<thead>
<tr>
<th>Id</th>
<th>Highway</th>
<th>State</th>
<th>Code</th>
<th>Rank</th>
<th>Created At</th>
<th>Updated At</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div id=editarea class='result' ></div>
以及资产管道的application.js:
//= require jquery
//= require jquery-ui
//= require jquery.dataTables
//= require TableTools
//= require ZeroClipboard
//= require datatable_highways
//= require datatable_localities
//= require datatable_neighbours
//= require_tree .
这可能是件简单的事情,但它已经让我耽搁了一段时间。谢谢你的期待
更新
DataTables调试器bookmarklet表示“列表”视图正在呈现以下内容,其中不包括DataTables所需的json参数
http://localhost:3000/highways.json
我认为应该是:
http://localhost:3000/highways/list.json.
我不确定这是控制器问题还是视图问题
更新
我做了各种改变,但仍然有同样的问题。但我在日志中看到两件重要的事情:
输入localhost:3000/highways/list显示渲染了两个视图
Processing by HighwaysController#list as HTML [1m[36mHighway Load (4.8ms)[0m [1mSELECT "highways".* FROM "highways" ORDER BY name
Highway Count: 229
Rendered highways/list.html.erb within layouts/highways (32.6ms)
然后
第一种是HTML(通过操作“列表”),第二种是JSON(通过操作“索引”)
因此有两个问题:
match 'highways/list'=> 'highways#list'
...
resources :highways
当我只想呈现视图(列标题等)时,如何停止数据请求?我需要通过什么途径将JSON响应发送到控制器中的“列表”操作?已解决。使用Ryan Bates#340代码返回到一个最小设置,1个表-因此使用索引操作/视图来解决上面的2个渲染问题。仍然不起作用。将gem删除到rweng github,并将DataTables文件加载到./vendor/assets。现在起作用了。索引或列表操作可能可以通过显式路由修复,但当我试图通过列表操作强制执行“缺少模板”时,我无法通过它。已解决。使用Ryan Bates#340代码返回到一个最小设置,1个表-因此使用索引操作/视图来解决上面的2个渲染问题。仍然不起作用。将gem删除到rweng github,并将DataTables文件加载到./vendor/assets。现在起作用了。索引或列表操作可能可以通过显式路由修复,但当我试图通过列表操作强制“缺少模板”时,我无法通过它。
class HighwaysController < ApplicationController
respond_to :html, :json
def index
@highways = Highway.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @highways }
end
end
def list
@highways = Highway.all
logger.debug "Highway Count: #{@highways.size}"
respond_to do |format|
format.html # index.html.erb
format.json { render json: HighwaysDatatable.new(view_context)}
end
end
http://localhost:3000/highways/list.json.
Processing by HighwaysController#list as HTML [1m[36mHighway Load (4.8ms)[0m [1mSELECT "highways".* FROM "highways" ORDER BY name
Highway Count: 229
Rendered highways/list.html.erb within layouts/highways (32.6ms)
Processing by HighwaysController#index as JSON
Parameters: {"sEcho"=>"1", "iColumns"=>"5", "sColumns"=>"", "iDisplayStart"=>"0",...
match 'highways/list'=> 'highways#list'
...
resources :highways