Rails Highchart不循环

时间:2013-11-30 01:24:57

标签: ruby-on-rails highcharts

我正在使用Lazy Highcharts从我的数据库生成图表。我有一个位于我的控制器中的循环,它正确地循环列,但它只列出最后一个输入的网站的类别,不知道为什么这样。

控制器

def index
  @nutritiontrials = Nutritiontrial.all

  @plantstand = LazyHighCharts::HighChart.new('graph') do |f|
    @nutritiontrials.each do |trial|
      f.xAxis(:categories => [trial.site])
      f.series(:type => 'column', :name => 'Plant Stand Treated', :data => [trial.nil_plant_stand_est], :color => '#00463f')
    end
  end
end

1 个答案:

答案 0 :(得分:1)

 @nutritiontrials = Nutritiontrial.all
    sites = []
    plant_stand = []

    @nutritiontrials.each do |trial|
      sites << trial.site
      plant_stand << trial.nil_plant_stand_est
    end  

    @plantstand = LazyHighCharts::HighChart.new('graph') do |f|
      f.xAxis(:categories => sites)
      f.series(:type => 'column', :name => 'Plant Stand Treated', :data => plant_stand, :color => '#00463f')
    end