ruby on rails drawing failed but how come -
in below code, trying draw bunch of chart. database has model person have attribute of "name,weight,height,color,age" objective draw chart each row, weight x-axis, height y-axis. , color color each chart, eg person 1 have color yellow, chart should yellow (this tricky implmented)
anyway, using lazy_high_chart implement this, no luck, failed no error.
but consistent, here comes 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 |x| names = x.name weights = x.weight heights = x.height colors = x.color ages = x.age end @chart = lazyhighcharts::highchart.new('graph') |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 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 |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 routes correct, should deal display action within controller code block. have read through lazy_high_chart example, seems simple , not related case. ideas? many thanks
yeah works out no data shown how come?? log updated, seems data extracted did not show up
you have infinite redirect. /people/display redirects /people/display.
remove line:
redirect_to :action => :display
you overwriting arrays in @people.each
loop. instead of =
should using <<
add values each array:
names = [] weights = [] heights = [] colors = [] ages = [] @people.each |x| names << x.name weights << x.weight heights << x.height colors << x.color ages << x.age end
Comments
Post a Comment