RMagic 轨道误差,带 AM 图表

发布于 2024-08-28 07:19:38 字数 2316 浏览 1 评论 0原文

我正在使用 AMCharts 和 Rails。 AMCharts 使用 Image Magic 库导出图表的图像。在 Rails 中,这是通过 gem RMagic 完成的。

在控制器中,这是通过以下控制器方法实现的:

  def export
    width = params[:width].to_i
    height = params[:height].to_i
    data = {}
    img = Magick::Image.new(width, height)
    height.times do |y|
      row = params["r#{y}"].split(',')
      row.size.times do |r|
        pixel = row[r].to_s.split(':')
        pixel[0] = pixel[0].to_s.rjust(6, '0')
        if pixel.size == 2
          pixel[1].to_i.times do
            (data[y] ||= []) << pixel[0]
          end
        else
          (data[y] ||= []) << pixel[0]
        end
      end
      width.times do |x|
        img.pixel_color(x, y, "##{data[y][x]}")
      end
    end
    img.format = "PNG"
    send_data(img.to_blob , :disposition => 'inline', :type => 'image/png', :filename => "chart.png?#{rand(99999999).to_i}")
  end

但是,当访问控制器时,我在页面中收到此错误:

The change you wanted was rejected.

Maybe you tried to change something you didn't have access to.

日志中出现此错误(顺便说一句,它在 Heroku 上运行):

    ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
  /home/heroku_rack/lib/static_assets.rb:9:in `call'
  /home/heroku_rack/lib/last_access.rb:25:in `call'
  /home/heroku_rack/lib/date_header.rb:14:in `call'
  thin (1.0.1) lib/thin/connection.rb:80:in `pre_process'
  thin (1.0.1) lib/thin/connection.rb:78:in `catch'
  thin (1.0.1) lib/thin/connection.rb:78:in `pre_process'
  thin (1.0.1) lib/thin/connection.rb:57:in `process'
  thin (1.0.1) lib/thin/connection.rb:42:in `receive_data'
  eventmachine (0.12.6) lib/eventmachine.rb:240:in `run_machine'
  eventmachine (0.12.6) lib/eventmachine.rb:240:in `run'
  thin (1.0.1) lib/thin/backends/base.rb:57:in `start'
  thin (1.0.1) lib/thin/server.rb:150:in `start'
  thin (1.0.1) lib/thin/controllers/controller.rb:80:in `start'
  thin (1.0.1) lib/thin/runner.rb:173:in `send'
  thin (1.0.1) lib/thin/runner.rb:173:in `run_command'
  thin (1.0.1) lib/thin/runner.rb:139:in `run!'
  thin (1.0.1) bin/thin:6
  /usr/local/bin/thin:20:in `load'
  /usr/local/bin/thin:20

Rendering /disk1/home/slugs/149903_609c236_eb4f/mnt/public/422.html (422 Unprocessable Entity)

有人知道这里发生了什么吗?

I'm using AMCharts and rails. AMCharts uses the Image Magic lib to export an image of the chart. In rails this is done with the gem, RMagic.

In a controller this is implemented with the following controller method:

  def export
    width = params[:width].to_i
    height = params[:height].to_i
    data = {}
    img = Magick::Image.new(width, height)
    height.times do |y|
      row = params["r#{y}"].split(',')
      row.size.times do |r|
        pixel = row[r].to_s.split(':')
        pixel[0] = pixel[0].to_s.rjust(6, '0')
        if pixel.size == 2
          pixel[1].to_i.times do
            (data[y] ||= []) << pixel[0]
          end
        else
          (data[y] ||= []) << pixel[0]
        end
      end
      width.times do |x|
        img.pixel_color(x, y, "##{data[y][x]}")
      end
    end
    img.format = "PNG"
    send_data(img.to_blob , :disposition => 'inline', :type => 'image/png', :filename => "chart.png?#{rand(99999999).to_i}")
  end

When the controller is accessed however, I receive this error in the page:

The change you wanted was rejected.

Maybe you tried to change something you didn't have access to.

And this error in the logs (its running on heroku btw):

    ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
  /home/heroku_rack/lib/static_assets.rb:9:in `call'
  /home/heroku_rack/lib/last_access.rb:25:in `call'
  /home/heroku_rack/lib/date_header.rb:14:in `call'
  thin (1.0.1) lib/thin/connection.rb:80:in `pre_process'
  thin (1.0.1) lib/thin/connection.rb:78:in `catch'
  thin (1.0.1) lib/thin/connection.rb:78:in `pre_process'
  thin (1.0.1) lib/thin/connection.rb:57:in `process'
  thin (1.0.1) lib/thin/connection.rb:42:in `receive_data'
  eventmachine (0.12.6) lib/eventmachine.rb:240:in `run_machine'
  eventmachine (0.12.6) lib/eventmachine.rb:240:in `run'
  thin (1.0.1) lib/thin/backends/base.rb:57:in `start'
  thin (1.0.1) lib/thin/server.rb:150:in `start'
  thin (1.0.1) lib/thin/controllers/controller.rb:80:in `start'
  thin (1.0.1) lib/thin/runner.rb:173:in `send'
  thin (1.0.1) lib/thin/runner.rb:173:in `run_command'
  thin (1.0.1) lib/thin/runner.rb:139:in `run!'
  thin (1.0.1) bin/thin:6
  /usr/local/bin/thin:20:in `load'
  /usr/local/bin/thin:20

Rendering /disk1/home/slugs/149903_609c236_eb4f/mnt/public/422.html (422 Unprocessable Entity)

Anyone have any idea what's going on here?

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

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

发布评论

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

评论(1

得不到的就毁灭 2024-09-04 07:19:38

我已经弄清楚了:

添加:需要“RMagick”

&

Skip_before_filter :verify_authenticity_token

到我的班级

I've figured it out:

added: require 'RMagick'

&

skip_before_filter :verify_authenticity_token

to my class

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