Server side processing hangs
Server side processing hangs
I am trying to get DataTables working at the genetics research institute where I work. After discovering DataTables at RailsCasts, I've followed Ryan Bate's example as far as I can, and tried to study more examples here, to no avail; my application just hangs and says "Processing" forever when I call it. I would really like to get it working though, as it seems the perfect plugin for our needs. However, I am new to Rails, and am at a loss as to how to troubleshoot the problem. (I am going to try and learn how to start using the DataTables debugger, which looks like it could be very helpful.)
Unfortunately, our website is not public, so I cannot provide a URL. However, I suspect the problem is in my /app/controllers/genotypes_controller.rb or my GenotypesDatatable class, which I've provided below; any helpful suggestions will be very much appreciated!
[code]
(here the class:)
class GenotypesDatatable
delegate :params, :h, :link_to, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
# This is what feeds directly into DataTables
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Genotype.count,
iTotalDisplayRecords: 10,
aaData: data
}
end
private
def data
genotypes.map do |genotype|
[
# Note: h is shorthand for html_escape
h(genotype.gmarker.marker),
h(genotype.gsample.labid),
h(genotype.gsample.subjectid),
h(genotype.gsample.box),
h(genotype.gsample.well),
h(genotype.allele1),
h(genotype.allele2),
h(genotype.run_date)
]
end
end
def genotypes
@genotypes ||= fetch_genotypes
end
def fetch_genotypes
genotypes = Genotype.order("#{sort_column} #{sort_direction}")
genotypes = genotypes.page(page).per_page(per_page)
if params[:sSearch].present?
genotypes = genotypes.where("labid like :search or category like :search", search: "%#{params[:sSearch]}%")
end
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[allele1 allele2 run_date]
columns[params[:iSortCol_0].to_i]
end
def sort_direction
params[:sSortDir_0] == "desc" ? "desc" : "asc"
end
end
(and here's the controller:)
class GenotypesController < ApplicationController
# GET /genotypes
# GET /genotypes.json
def index
respond_to do |format|
format.html
format.json { render json: GenotypesDatatable.new(view_context) }
end
end
# GET /genotypes/1
# GET /genotypes/1.json
def show
@genotype = Genotype.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @genotype }
end
end
# GET /genotypes/new
# GET /genotypes/new.json
def new
@genotype = Genotype.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @genotype }
end
end
# GET /genotypes/1/edit
def edit
@genotype = Genotype.find(params[:id])
end
# POST /genotypes
# POST /genotypes.json
def create
@genotype = Genotype.new(params[:genotype])
respond_to do |format|
if @genotype.save
format.html { redirect_to @genotype, notice: 'Genotype was successfully created.' }
format.json { render json: @genotype, status: :created, location: @genotype }
else
format.html { render action: "new" }
format.json { render json: @genotype.errors, status: :unprocessable_entity }
end
end
end
# PUT /genotypes/1
# PUT /genotypes/1.json
def update
@genotype = Genotype.find(params[:id])
respond_to do |format|
if @genotype.update_attributes(params[:genotype])
format.html { redirect_to @genotype, notice: 'Genotype was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @genotype.errors, status: :unprocessable_entity }
end
end
end
# DELETE /genotypes/1
# DELETE /genotypes/1.json
def destroy
@genotype = Genotype.find(params[:id])
@genotype.destroy
respond_to do |format|
format.html { redirect_to genotypes_url }
format.json { head :no_content }
end
end
end
[/code]
Unfortunately, our website is not public, so I cannot provide a URL. However, I suspect the problem is in my /app/controllers/genotypes_controller.rb or my GenotypesDatatable class, which I've provided below; any helpful suggestions will be very much appreciated!
[code]
(here the class:)
class GenotypesDatatable
delegate :params, :h, :link_to, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
# This is what feeds directly into DataTables
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Genotype.count,
iTotalDisplayRecords: 10,
aaData: data
}
end
private
def data
genotypes.map do |genotype|
[
# Note: h is shorthand for html_escape
h(genotype.gmarker.marker),
h(genotype.gsample.labid),
h(genotype.gsample.subjectid),
h(genotype.gsample.box),
h(genotype.gsample.well),
h(genotype.allele1),
h(genotype.allele2),
h(genotype.run_date)
]
end
end
def genotypes
@genotypes ||= fetch_genotypes
end
def fetch_genotypes
genotypes = Genotype.order("#{sort_column} #{sort_direction}")
genotypes = genotypes.page(page).per_page(per_page)
if params[:sSearch].present?
genotypes = genotypes.where("labid like :search or category like :search", search: "%#{params[:sSearch]}%")
end
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[allele1 allele2 run_date]
columns[params[:iSortCol_0].to_i]
end
def sort_direction
params[:sSortDir_0] == "desc" ? "desc" : "asc"
end
end
(and here's the controller:)
class GenotypesController < ApplicationController
# GET /genotypes
# GET /genotypes.json
def index
respond_to do |format|
format.html
format.json { render json: GenotypesDatatable.new(view_context) }
end
end
# GET /genotypes/1
# GET /genotypes/1.json
def show
@genotype = Genotype.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @genotype }
end
end
# GET /genotypes/new
# GET /genotypes/new.json
def new
@genotype = Genotype.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @genotype }
end
end
# GET /genotypes/1/edit
def edit
@genotype = Genotype.find(params[:id])
end
# POST /genotypes
# POST /genotypes.json
def create
@genotype = Genotype.new(params[:genotype])
respond_to do |format|
if @genotype.save
format.html { redirect_to @genotype, notice: 'Genotype was successfully created.' }
format.json { render json: @genotype, status: :created, location: @genotype }
else
format.html { render action: "new" }
format.json { render json: @genotype.errors, status: :unprocessable_entity }
end
end
end
# PUT /genotypes/1
# PUT /genotypes/1.json
def update
@genotype = Genotype.find(params[:id])
respond_to do |format|
if @genotype.update_attributes(params[:genotype])
format.html { redirect_to @genotype, notice: 'Genotype was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @genotype.errors, status: :unprocessable_entity }
end
end
end
# DELETE /genotypes/1
# DELETE /genotypes/1.json
def destroy
@genotype = Genotype.find(params[:id])
@genotype.destroy
respond_to do |format|
format.html { redirect_to genotypes_url }
format.json { head :no_content }
end
end
end
[/code]
This discussion has been closed.
Replies
That will let us see the JSON return front he server and tell you where it is going wrong.
Allan