【发布时间】:2017-03-23 21:27:59
【问题描述】:
我正在为我的简单应用程序创建一个 pdf 收据。我能够使其适用于通用/主要是硬编码的条目。但是,当我希望 pdf 打印出特定费用的金额(非硬编码)时,我在 charge_controller.rb 文件中收到此错误 - “未定义的局部变量或方法 'amount' for #”我在下面列出我的特定 github 以及下面的相关内容。非常感谢你们的帮助:)
确切的错误信息:
未初始化的常量 Charge::Amount
app/models/charge.rb:18:in receipt'
app/controllers/charges_controller.rb:64:inblock (2 个级别) in show'
app/controllers/charges_controller.rb:60:in `show'
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: "teachers@onemonth.com",
},
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: "teachers@onemonth.com",
},
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
【问题讨论】:
-
请查看Posting Good Questions,它提供了详细有用的说明来帮助我们帮助您。错误指的是哪一行?请发布完整的错误,您太快将其切断。请同时格式化您的代码。
-
我很确定您还没有发布错误源自的代码部分。您能否发布原始错误,包括它来自哪个文件?\
-
max 和 Tyler 非常感谢您的 cmets,对于提出问题的方式,我深表歉意。我刚刚输入了完整的原始错误消息以及charge.rb(当它工作并给我一个pdf时)和一个charge.rb(我想要但给了我一个错误)。再次感谢您
标签: ruby-on-rails ruby pdf pdf-generation