我正在尝试按照 Railscasts 教程 340 从服务器端 JSON 数据填充 JQuery Datatables 组件。该教程的链接是:http ://railscasts.com/episodes/340-datatables 。我正在自定义代码以使用我自己的数据库表和数据。
当我尝试访问应该将 JSON 输出到数据表的链接时,我收到以下错误:
not opened for reading
控制器中处理请求的代码如下:
class DatadescriptionController < ApplicationController
layout "datadescription"
def index
respond_to do |format|
format.html
format.json { render json: DatasetIndexDatatable.new(view_context) }
end
end
end
在 format.json 命令中实例化的 DatasetIndexDatatable 类如下所示:
class DatasetIndexDatatable
delegate :params, :h, :link_to, :number_to_currency, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Datadescription.count,
iTotalDisplayRecords: datasets.total_entries,
aaData: data
}
end
private
def data
datasets.map do |dataset|
[
id,
database,
table,
created_by,
name
]
end
end
def datasets
@datasets ||= fetch_datasets
end
def fetch_datasets
datasets = Datadescription.order("#{sort_column} #{sort_direction}")
datasets = datasets.page(page).per_page(per_page)
if params[:sSearch].present?
datasets = datasets.where("name like :search or created_by like :search", search: "%# {params[:sSearch]}%")
end
datasets
end
def page
params[: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[id database table created_by name]
columns[params[:iSortCol_0].to_i]
end
def sort_direction
params[:sSortDir_0] == "desc" ? "desc" : "asc"
end
end
任何人都可以阐明这个“未打开以供阅读”错误的含义以及我如何更正我的代码以按预期输出 json?