使用大虾时显示未初始化的常量 Prawn::FLOAT_PRECISION 错误
uninitialized constant Prawn::FLOAT_PRECISION error showing while using prawn
我采用了 Railscast episode 153 revised 的方法。
我的控制器是
class AdminsController < ApplicationController
def index
@examples = Example.all
respond_to do |format|
format.html
format.csv { send_data @examples.to_csv }
format.xls { send_data @examples.to_csv }
format.pdf do
pdf = DownloadPdf.new(@examples)
send_data pdf.render, filename: 'generate_table.pdf',
type: 'application/pdf', disposition: "inline"
end
end
end
end
我的 download_pdf.rb 文件是
class DownloadPdf < Prawn::Document#make_table
require 'prawn/table'
def initialize(example)
super()
@examples = example
line_items
end
def line_items
image "#{Rails.root}/app/assets/images/logo.png"
table [[1,2],[3,4]]
end
end
我正在使用宝石
gem 'prawn', :git => "https://github.com/prawnpdf/prawn.git", :ref => '8028ca0cd2'
gem 'prawn-table', '~> 0.1.0'
在此先感谢您的帮助。
TL;DR:更新 prawn
gem,将其添加到您的 Gemfile 中:gem 'prawn'
和 运行 bundle install
.
更长的答案:您使用的是旧版本的 Prawn - 您在 Gemfile
中使用的 ref
指的是 2013 年的某个地方。prawn-table 0.1
较新,需要 prawn
的较新版本。更准确地说,它使用 Prawn
的 ::FLOAT_PRECISION
常量,该常量在 this 2014's commit 中添加到 Prawn
。
请使用下面的 Gemfile
gem 'prawn'
然后删除 Gemfile.lock
然后是
bundle install
重启服务器
你可以像这样使用round(2)
..
val= 456.7890999999
val.round(2)
答案将是 456.79
我采用了 Railscast episode 153 revised 的方法。
我的控制器是
class AdminsController < ApplicationController
def index
@examples = Example.all
respond_to do |format|
format.html
format.csv { send_data @examples.to_csv }
format.xls { send_data @examples.to_csv }
format.pdf do
pdf = DownloadPdf.new(@examples)
send_data pdf.render, filename: 'generate_table.pdf',
type: 'application/pdf', disposition: "inline"
end
end
end
end
我的 download_pdf.rb 文件是
class DownloadPdf < Prawn::Document#make_table
require 'prawn/table'
def initialize(example)
super()
@examples = example
line_items
end
def line_items
image "#{Rails.root}/app/assets/images/logo.png"
table [[1,2],[3,4]]
end
end
我正在使用宝石
gem 'prawn', :git => "https://github.com/prawnpdf/prawn.git", :ref => '8028ca0cd2' gem 'prawn-table', '~> 0.1.0'
在此先感谢您的帮助。
TL;DR:更新 prawn
gem,将其添加到您的 Gemfile 中:gem 'prawn'
和 运行 bundle install
.
更长的答案:您使用的是旧版本的 Prawn - 您在 Gemfile
中使用的 ref
指的是 2013 年的某个地方。prawn-table 0.1
较新,需要 prawn
的较新版本。更准确地说,它使用 Prawn
的 ::FLOAT_PRECISION
常量,该常量在 this 2014's commit 中添加到 Prawn
。
请使用下面的 Gemfile
gem 'prawn'
然后删除 Gemfile.lock
然后是
bundle install
重启服务器
你可以像这样使用round(2)
..
val= 456.7890999999
val.round(2)
答案将是 456.79