2016-01-27 164 views
0

,我想繪製一堆圖表。 我的數據庫中有模型名稱爲「名稱,重量,身高,顏色,年齡」的人 所以我的目標是爲每一行繪製圖表,其中x軸爲權重,y軸爲高度。並且顏色將是每個圖表的實際顏色,例如,人員1的顏色是黃色,那麼圖表應該是黃色的(這是非常棘手的問題)在軌道上繪製紅寶石失敗,但如何在下面的代碼

無論如何,我正在使用lazy_high_chart來執行此操作,但沒有運氣,我失敗了,沒有錯誤。

不過是一致的,這纔是我的代碼: people_controller.rb

class PeopleController < ApplicationController 
    #GET /people/index 
    #GET /people 
    def index 
     @people = Person.all 
    end 

    #GET /people/show/id 
    def show 
     @person = Person.find(params[:id]) 
    end 

    #GET /people/new 
    def new 
     @person = Person.new 
    end 

    #POST /people/update 
    def create 
     @person = Person.new(person_params) 
     @person.save 
     redirect_to :action => :index 
    end 

    #GET /people/edit/:id 
    def edit 
     @person = Person.find(params[:id]) 
    end 

    #POST /people/update/:id 
    def update 
     @person = Person.find(params[:id]) 
     @person.update(person_params) 
     redirect_to :action => :show, :id => @person 
    end 

    #GET /people/destroy/:id 
    def destroy 
     @person = Person.find(params[:id]) 
     @person.destroy 
     redirect_to :action => :index 
    end 

    def display 
     @people = Person.all 
     names = [] 
     weights = [] 
     heights = [] 
     colors = [] 
     ages = [] 
     @people.each do |x| 
      names = x.name 
      weights = x.weight 
      heights = x.height 
      colors = x.color 
     ages = x.age 
     end 

     @chart = LazyHighCharts::HighChart.new('graph') do |x| 
      x.title(:text => "Display Data") 
      x.xAxis(:categories => weights, :title => names, :margin => 10) 
      x.yAxis(:categories => heights) 
      x.series(:type => 'column', :name => 'showing data', :data => weights, :color => colors) 
     end 
    end 

    private 
    def person_params 
     params.require(:person).permit(:name, :weight, :height, :color, :age) 
    end 
end 

的routes.rb

Rails.application.routes.draw do 
    root 'people#index' 
    match ':controller(/:action(/:id(.:format)))', :via => :all 
end 

index.html.erb:

<h1> People list</h1> 
<table> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th> Weight</th> 
      <th> Height</th> 
      <th> Color</th> 
      <th> Age</th> 
      <th colspan="4"></th> 
     </tr> 
    </thead> 
    <tbody> 
     <% @people.each do |e| %> 
     <tr> 
      <td><%= e.name %></td> 
      <td><%= e.weight %></td> 
      <td><%= e.height %></td> 
      <td><%= e.color %></td> 
      <td><%= e.age %></td> 
      <td><%= link_to 'Show', :controller => "people", :action => "show", :id => e %></td> 
      <td><%= link_to 'Edit', :controller => "people", :action => "edit", :id => e %></td> 
      <td><%= link_to 'Delete', :controller => "people", :action => "destroy", :id => e %></td> 
     </tr> 
     <% end %> 
    </tbody> 
</table> 
<br> 
<%= link_to 'New Input', :controller => 'people', :action => 'new' %> 
<%= link_to 'Display', :controller => "people", :action => "display" %> 

顯示。 html.erb:

<h1> Display Result</h1> 
<%= high_chart("display_res", @chart) %> 

我相信我的路線是正確的,應該處理控制器代碼塊中的顯示動作。 我已閱讀lazy_high_chart示例,但似乎太簡單,並且與我的案例無關。 有什麼建議嗎?非常感謝

呀它的作品了,但展示瞭如何來沒有數據? 日誌已更新,似乎數據已提取但未顯示

+0

看:什麼是實際發生的? –

+0

剛剛更新日誌 – ThugForever

回答

1

您擁有無限重定向。/people/display重定向到/ people/display。

刪除此行:

redirect_to :action => :display

您還覆蓋陣列中的@people.each循環。取而代之的=你應該使用<<將值添加到每個陣列:在您的記錄

names = [] 
    weights = [] 
    heights = [] 
    colors = [] 
    ages = [] 
    @people.each do |x| 
     names << x.name 
     weights << x.weight 
     heights << x.height 
     colors << x.color 
     ages << x.age 
    end 
+0

感謝您的幫助,現在它的工作,但沒有數據繪製 – ThugForever

+1

您可能在您的日誌中有新的信息。看看那裏是否有任何有用的信息。祝你好運。 – msergeant