我正在努力在我的简单应用程序上创建pdf收据。我能够使它适用于通用/大多数硬编码条目。但是,当我希望pdf打印出特定费用的金额(不是硬编码)时,我在charge_controller.rb文件中出现此错误 - “未定义的局部变量或方法`金额”为#“我在下面列出我的具体github以及下面的相关内容。非常感谢你们的帮助:)
确切的错误消息:
未初始化的恒定费用::金额
app / models / charge.rb:18:in receipt'
app/controllers/charges_controller.rb:64:in
块(2级)in show'
app / controllers / charges_controller.rb:60:在'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
答案 0 :(得分:1)
您的模型中有一个大写Amount
,它将引用一个常量。它应该是小写的amount
来代替费用记录中的属性。
我认为应该是:["Amount", "$#{amount / 100}.00"]