ruby on rails drawing failed but how come

95 Views Asked by At

in the below code, i am trying to draw bunch of chart. My database has the model Person which have the attribute of "name,weight,height,color,age" So my objective is to draw the chart for each row, with weight as the x-axis, height as the y-axis. And color would be the actually color for the each chart, for eg person 1 have color yellow, then the chart should be yellow (this is very tricky to implmented)

Anyway, i am using the lazy_high_chart to implement this, but with no luck, i failed with no error.

But to be consistent, here comes my code: 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" %>

display.html.erb:

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

I believe my routes are correct, it should be deal with the display action within the controller code block. I have read through the lazy_high_chart example, but seems too simple and not related to my case. Any ideas? many thanks

Yeah it works out but no data shown how come?? Log updated, seems the data are extracted but did not show up

1

There are 1 best solutions below

2
On

You have an infinite redirect. /people/display redirects to /people/display.

Remove this line:

redirect_to :action => :display

You are also overwriting the arrays in your @people.each loop. Instead of = you should be using << to add values to each array:

    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