ruby-on-railsloopscontrolleropen-flash-chart

A simple controller loop (including open flash chart)


I have a simple problem regarding a loop in a Rails controller.

Here's the original sample code, the purpose of which is to specify the data to be used in an open flash chart (pie chart).

#controller
data_1 = [
  OFC2::PieValue.new(:value => 20,  :label => 'GroupA', :font_size => 15),
  OFC2::PieValue.new(:value => 30, :label =>  'GroupB', :font_size => 15)
]

I need to do this:

data_1 = [
  @groups.each do |group|
    OFC2::PieValue.new(:value => group.value,  :label => group.name, :font_size => 15),
  end
]

Two questions:

  1. The comma at the end of that line poses a problem. The last entry cannot have a comma.
  2. Even when I try to get this simple loop working by temporarily bypassing the comma (such as adding another record after the end without a comma), I am getting errors:

    unexpected ',', expecting kEND (for OFC2 line)
    unexpected ']', expecting kEND (last line of above code)
    unexpected kEND, expecting ']' (end of controller)

This is bugging me because it should be a simple loop. What's going on?


Solution

  • Maybe try going a different route.

    data_1 = Array.new
    
    @groups.each do |g|
      data_1 << OFC2::PieValue.new(:value => g.value,  :label => g.name, :font_size => 15)
    end
    

    Does this make sense?