我正在创建我的简单应用程序上的pdf收据。我能够使它适用于通用/大多数是硬编码的条目。但是,当我希望PDF打印出特定费用(不是硬编码)的金额时,我在我的charges_controller.rb文件上得到了这个错误 - “未定义的局部变量或方法'金额'为#”我列在下面我的具体github以及下面的相关内容。非常感谢你的帮助球员:)在Rails上从头创建PDF
确切的错误信息:
未初始化的恒流充电::金额 应用程序/模型/ charge.rb:18:在节目” receipt' app/controllers/charges_controller.rb:64:in
块(2级)应用程序/控制器/ charges_controller.rb:60:在`显示”
Github上 - https://github.com/OmarZV/PDF
charges_controller.rb
class ChargesController < ApplicationController
before_action :authenticate_user!
before_action :set_charge, only: [:show]
def index
@charges = Charge.all
end
def new
@charge = Charge.new
end
def create
@charge = Charge.new(charge_params)
respond_to do |format|
if @charge.save
format.html { redirect_to @charge, notice: 'Charge was successfully created.' }
format.json { render :show, status: :created, location: @charge }
else
format.html { render :new }
format.json { render json: @charge.errors, status: :unprocessable_entity }
end
end
end
def show
respond_to do |format|
format.html
format.json
format.pdf {
send_data @charge.receipt.render,
filename: "#{@charge.created_at.strftime("%Y-%m-%d")}-gorails-receipt.pdf",
type: "application/pdf",
disposition: :inline
}
end
end
private
def set_charge
@charge = current_user.charges.find(params[:id])
end
end
charge.rb当它工作
class Charge < ApplicationRecord
belongs_to :user
def receipt
Receipts::Receipt.new(
id: id,
product: "GoRails",
company: {
name: "One Month, Inc.",
address: "37 Great Jones\nFloor 2\nNew York City, NY 10012",
email: "[email protected]",
},
line_items: [
["Date", created_at.to_s],
["Account Billed", "(#{user.email})"],
["Product", "GoRails"],
["Amount", "Amount"],
["Charged to", "{Card_type}"],
]
)
end
end
charge.rb当它不工作
class Charge < ApplicationRecord
belongs_to :user
def receipt
Receipts::Receipt.new(
id: id,
product: "GoRails",
company: {
name: "One Month, Inc.",
address: "37 Great Jones\nFloor 2\nNew York City, NY 10012",
email: "[email protected]",
},
line_items: [
["Date", created_at.to_s],
["Account Billed", "(#{user.email})"],
["Product", "GoRails"],
["Amount", "$#{Amount/100}.00"],
["Charged to", "#{Card_type} (**** **** **** #{Card_last4})"],
]
)
end
end
user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable
has_many :charges
end
请看看[发布好问题(http://stackoverflow.com/help/how-to-ask)里面有详细的和有用的指导,帮助我们帮你。这个错误指的是什么?请发布完整的错误,你过早关闭它。请格式化您的代码。 – Genzume
我很确定你没有发布错误源自的代码部分。您能否发布原始错误,包括哪些文件来自?\ –
max,泰勒非常感谢您的评论,我对提出问题的方式表示歉意。我刚刚输入了完整的原始错误信息以及charge.rb(当它工作并给了我一个pdf)和一个charge.rb(我想要的但给了我一个错误)。再次感谢您 – Omar