【问题标题】:ruby on rails drawing failed but how comeruby on rails 绘图失败,但怎么会
【发布时间】:2016-01-27 12:17:21
【问题描述】:

在下面的代码中,我正在尝试绘制一堆图表。 我的数据库有模型 Person,它具有“名称、体重、身高、颜色、年龄”的属性 所以我的目标是为每一行绘制图表,以重量为 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" %>

display.html.erb:

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

我相信我的路线是正确的,应该是处理控制器代码块中的显示动作。 我已经阅读了lazy_high_chart 示例,但似乎太简单且与我的情况无关。 有任何想法吗?非常感谢

是的,它成功了,但没有数据显示是怎么回事? 日志已更新,似乎数据已提取但未显示

【问题讨论】:

  • 查看您的日志:实际发生了什么?
  • 刚刚用日志更新了

标签: ruby-on-rails ruby-on-rails-4 highcharts lazy-high-charts


【解决方案1】:

您有一个无限重定向。 /people/display 重定向到 /people/display。

删除这一行:

redirect_to :action =&gt; :display

您还覆盖了@people.each 循环中的数组。您应该使用 &lt;&lt; 而不是 = 向每个数组添加值:

    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

【讨论】:

  • 感谢您的帮助,现在它可以正常工作,但没有绘制数据
  • 您的日志中可能有新信息。看看里面有没有有用的信息。祝你好运。
猜你喜欢
  • 1970-01-01
  • 2012-01-07
  • 2014-06-03
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多