未定义的方法“收集”对于 2:Fixnum

发布于 2024-12-18 09:57:23 字数 2686 浏览 3 评论 0原文

def function1
   @user=User.find(params[:user_id])
   @participants=Participant.find_all_by_user_id(params[:user_id])
    bar_1_data = @user.get_total
    bar_2_data = params[:count]
    color_1 = 'c53711'
    color_2 = '0000ff'
    min=0
    max=100
    puts(bar_1_data)      # prints 2 on console
    puts(bar_2_data)      # prints 3 on console
    puts (bar_1_data).is_a? Integer # prints true
    puts (bar_2_data).is_a? Integer # prints false
    bc=GoogleChart::BarChart.new("300x80", " ", :horizontal, false)
    bc.data " ", [100], 'ffffff'
    bc.data "Bar1", bar_1_data, color_1
    bc.data "Bar2", bar_2_data.to_i, color_2
    bc.axis :x, :range => [min,max]
    bc.show_legend = true
    bc.stacked = false
    bc.data_encoding = :extended
    @bc= bc.to_url
    end
  end

我在上面的控制器代码中收到参数错误“字符串与 100 的比较失败”。然后我更改了

bc.data "Bar1", bar_1_data, color_1
bc.data "Bar2", bar_2_data, color_2

bc.data "Bar1", bar_1_data.to_i, color_1
bc.data "Bar2", bar_2_data.to_i, color_2

@bc= bc.to_url 行为我提供 undefined methodcollect for 2:Fixnum 的行 在控制台上,它给出以下错误:

NoMethodError (undefined method `collect' for 2:Fixnum):
  gchartrb (0.8) lib/google_chart/base.rb:499:in `extended_encode'
  gchartrb (0.8) lib/google_chart/base.rb:461:in `encode_data'
  gchartrb (0.8) lib/google_chart/bar_chart.rb:69:in `process_data'
  gchartrb (0.8) lib/google_chart/bar_chart.rb:68:in `collect'
  gchartrb (0.8) lib/google_chart/bar_chart.rb:68:in `process_data'
  gchartrb (0.8) lib/google_chart/base.rb:440:in `add_data'
  gchartrb (0.8) lib/google_chart/base.rb:305:in `prepare_params'
  gchartrb (0.8) lib/google_chart/base.rb:77:in `to_url'
  app/controllers/assess_controller.rb:139:in `function1'
  gchartrb (0.8) lib/google_chart/bar_chart.rb:22:in `initialize'
  app/controllers/assess_controller.rb:131:in `new'
  app/controllers/assess_controller.rb:131:in `function1'
  /Library/Ruby/Gems/1.8/gems/hoptoad_notifier-2.4.11/lib/hoptoad_notifier/rack.rb:27:in `call'
  /Library/Ruby/Gems/1.8/gems/hoptoad_notifier-2.4.11/lib/hoptoad_notifier/user_informer.rb:12:in `call'

Rendering rescues/layout (internal_server_error)

尝试在视图中将 bc 打印为

,它不打印任何内容,但是当我在控制器中打印 bc 时,它会打印 #

我没有正确地将其发送到视图吗?我已经尝试了 @bc=bc.to_urlputs bc.to_urlhttp://gchartrb.rubyforge.org/

我缺少什么?

def function1
   @user=User.find(params[:user_id])
   @participants=Participant.find_all_by_user_id(params[:user_id])
    bar_1_data = @user.get_total
    bar_2_data = params[:count]
    color_1 = 'c53711'
    color_2 = '0000ff'
    min=0
    max=100
    puts(bar_1_data)      # prints 2 on console
    puts(bar_2_data)      # prints 3 on console
    puts (bar_1_data).is_a? Integer # prints true
    puts (bar_2_data).is_a? Integer # prints false
    bc=GoogleChart::BarChart.new("300x80", " ", :horizontal, false)
    bc.data " ", [100], 'ffffff'
    bc.data "Bar1", bar_1_data, color_1
    bc.data "Bar2", bar_2_data.to_i, color_2
    bc.axis :x, :range => [min,max]
    bc.show_legend = true
    bc.stacked = false
    bc.data_encoding = :extended
    @bc= bc.to_url
    end
  end

I am getting the argument error "comparison of String with 100 failed" int the above controller code. Then I changed the lines

bc.data "Bar1", bar_1_data, color_1
bc.data "Bar2", bar_2_data, color_2

to

bc.data "Bar1", bar_1_data.to_i, color_1
bc.data "Bar2", bar_2_data.to_i, color_2

which gives me undefined method collect for 2:Fixnum at line @bc= bc.to_url.
On console it gives the following error:

NoMethodError (undefined method `collect' for 2:Fixnum):
  gchartrb (0.8) lib/google_chart/base.rb:499:in `extended_encode'
  gchartrb (0.8) lib/google_chart/base.rb:461:in `encode_data'
  gchartrb (0.8) lib/google_chart/bar_chart.rb:69:in `process_data'
  gchartrb (0.8) lib/google_chart/bar_chart.rb:68:in `collect'
  gchartrb (0.8) lib/google_chart/bar_chart.rb:68:in `process_data'
  gchartrb (0.8) lib/google_chart/base.rb:440:in `add_data'
  gchartrb (0.8) lib/google_chart/base.rb:305:in `prepare_params'
  gchartrb (0.8) lib/google_chart/base.rb:77:in `to_url'
  app/controllers/assess_controller.rb:139:in `function1'
  gchartrb (0.8) lib/google_chart/bar_chart.rb:22:in `initialize'
  app/controllers/assess_controller.rb:131:in `new'
  app/controllers/assess_controller.rb:131:in `function1'
  /Library/Ruby/Gems/1.8/gems/hoptoad_notifier-2.4.11/lib/hoptoad_notifier/rack.rb:27:in `call'
  /Library/Ruby/Gems/1.8/gems/hoptoad_notifier-2.4.11/lib/hoptoad_notifier/user_informer.rb:12:in `call'

Rendering rescues/layout (internal_server_error)

Tried printing bc in the view as <p><img src="<%= puts (@bc) %>" ></p>,it doesn't print anything, but when I am printing bc in the controller it prints
#<GoogleChart::BarChart:0x103be9660>.

Am I not sending it correctly to the view? I have tried both @bc=bc.to_url and puts bc.to_url as in http://gchartrb.rubyforge.org/.

What am I missing?

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

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

发布评论

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

评论(2

夏の忆 2024-12-25 09:57:23

在不知道谷歌图表的 api 的情况下,我看到了这一点:

bc.data " ", [100], 'ffffff'
bc.data "Bar1", bar_1_data, color_1
bc.data "Bar2", bar_2_data.to_i, color_2

第一行的参数是 String、Array、String 类型。

接下来两个的参数是 String、Fixnum、String

您的错误消息是 undefined methodcollect for 2:Fixnum ...所以也许您应该将 Fixnum 更改为 Array。

bc.data " ", [100], 'ffffff'
bc.data "Bar1", [bar_1_data], color_1
bc.data "Bar2", [bar_2_data.to_i], color_2

Without knowing the api for google charts, I see this:

bc.data " ", [100], 'ffffff'
bc.data "Bar1", bar_1_data, color_1
bc.data "Bar2", bar_2_data.to_i, color_2

The parameters of the first line are of type String, Array, String.

The parameters of the next two are String, Fixnum, String

Your error message is undefined method collect for 2:Fixnum ... so perhaps you should change the Fixnum to Array.

bc.data " ", [100], 'ffffff'
bc.data "Bar1", [bar_1_data], color_1
bc.data "Bar2", [bar_2_data.to_i], color_2
四叶草在未来唯美盛开 2024-12-25 09:57:23

看起来您正在将 bar_1_data 和 bar_2_data 加载到数组中。因此,错误未定义方法 to_i for Array

而不是:

bar_1_data = [@user.get_total]
bar_2_data = [params[:count]]

尝试一下

bar_1_data = @user.get_total
bar_2_data = params[:count]

请注意,将数据放在 [] 括号内意味着它现在是一个包含一项的数组。

It looks like you're loading bar_1_data and bar_2_data into arrays. Thus the error undefined method to_i for Array.

Instead of:

bar_1_data = [@user.get_total]
bar_2_data = [params[:count]]

Just try

bar_1_data = @user.get_total
bar_2_data = params[:count]

Note that putting your data inside [] brackets means it's now an array of one item.

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