一个简单的控制器循环(包括打开的Flash图表)

发布于 2024-09-15 03:45:18 字数 746 浏览 3 评论 0原文

我有一个关于 Rails 控制器中循环的简单问题。

这是原始示例代码,其目的是指定要在打开的 Flash 图表(饼图)中使用的数据。

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

我需要这样做:

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

两个问题:

  1. 该行末尾的逗号提出了问题。最后一个条目不能有逗号。
  2. 即使我尝试通过暂时绕过逗号来让这个简单的循环工作(例如在末尾添加另一条记录而不使用逗号),我也会收到错误:

    意外的“,”,需要 kEND(对于 OFC2 线路)
    意外的 ']',需要 kEND(上面代码的最后一行)
    意外的 kEND,期望 ']' (控制器结束)

这让我很烦恼,因为它应该是一个简单的循环。这是怎么回事?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捎一片雪花 2024-09-22 03:45:18

也许尝试走不同的路线。

data_1 = Array.new

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

这有道理吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文