2

この単純な必要性に対する答えを見つけることができないようです: 関連するデータテーブルについて Rails、したがって DataTables に伝える方法は? これはRailscast 340に基づいています。(Ryan Bates は、サーバー側の処理はより複雑であると警告していますが、実際にそうです!)

私はまだ Rails の経験があまりないので、Active Record を使用して関連データについて ROR に伝えるためのさまざまな find() メソッドについてまだ学んでいます。しかし、私の仕事の本質は、遺伝子型の index.html.erb ビュー ファイルに表示することです。

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

GenotypesDatatables の完全なクラスについては、以下を参照してください。これは、/app フォルダーの直下にある新しいクラスに配置されます。

遺伝子型、gmarkers、gvision の 3 つのモデルのデータを表示/編集する必要があります。gsamples と gmarkers から、遺伝子型に関連する 2 つのモデルのデータも表示する必要があります。

モデルは次のように構築されます。

class Gmarker < ActiveRecord::Base
attr_accessible :marker
has_many :genotypes, :dependent => :delete_all

...
class Genotype < ActiveRecord::Base
attr_accessible :allele1, :allele2, :run_date
belongs_to :gmarkers
belongs_to :gsamples
...
class Gsample < ActiveRecord::Base
belongs_to :gupload
has_many :genotypes, :dependent => :delete_all
attr_accessible :box, :labid, :subjectid, :well

Web サーバーから出力されたこのエラー メッセージに見られるように、問題は正しいデータ関連付けを取得していないことにあります。

CACHE (0.0ms) SELECT COUNT(*) FROM "genotypes"
Genotype Load (10.5ms) SELECT "genotypes".* FROM "genotypes" ORDER BY allele1 asc LIMIT 10 OFFSET 0
Completed 500 Internal Server Error in 255ms

NameError (undefined local variable or method `f' for #<GenotypesDatatable:0x9833ad4>):
app/datatables/genotypes_datatable.rb:24:in `block in data'
app/datatables/genotypes_datatable.rb:21:in `data'
app/datatables/genotypes_datatable.rb:14:in `as_json'
app/controllers/genotypes_controller.rb:7:in `block (2 levels) in index'
app/controllers/genotypes_controller.rb:5:in `index'
...

データはサーバー側の処理を使用して準備されることになっていますが、その結果、JSON 配列が DataTables の jQuery に渡されます。JSON 配列はクラス DataTables で準備されます。

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: genotypes.total_entries,
      aaData: data
    }
  end

private

  def data
    genotypes.map do |genotype|
      [
        # Note: h is shorthand for html_escape
        h(Gmarker.find(f.gmarkers_id).marker),
        h(Gsample.find(f.gsamples_id).labid),
        h(Gsample.find(f.gsamples_id).subjectid),
        h(Gsample.find(f.gsamples_id).box),
        h(Gsample.find(f.gsamples_id).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
    genotypes
  end
...

ここでの指針をいただければ幸いです。地図も懐中電灯も持たずにジャングルで迷子になった気分!

ありがとう、リック・ケーシー

4

1 に答える 1

2

ご存知のように、私も同様のことをしています。おそらく問題を混乱させるコードを変更しようとするのではなく、私のコードを例として投稿します。テーブルの隣人に関連付けが含まれています。

class Neighbour < ActiveRecord::Base

  belongs_to :locality, :class_name => "Locality"
  belongs_to :neighbour, :class_name => "Locality"
  belongs_to :highway,  :class_name => "Highway"

  default_scope joins(:locality, :highway).order('localities.name')

end

この議論では default_scope は関係ないと思います。

私が思うのは、NeighboursDatatable クラスの関連コードです。

def data
  neighbours.map do |neighbour|
    [
     neighbour.locality.name,
     neighbour.neighbour.name,        
     neighbour.highway.name, 
     neighbour.distance,
     neighbour.id
    ]
 end

終わり

したがって、明示的な検索を行う必要があるかどうかはわかりません。私のテーブルは、DataTables を使用して正しく表示されます。これが別のROR初心者から役立つことを願っています.

ジョン

于 2012-08-06T09:42:10.040 に答える