![](/img/trans.png)
[英]Creating a follow/unfollow relationship from scratch between two models in Rails
[英]Creating PDFs from scratch on Rails
我正在通過簡單的應用程序創建pdf收據。 我能夠使它適用於通用/主要是硬編碼的條目。 但是,當我希望pdf打印出特定費用的金額(不是硬編碼)時,我在charges_controller.rb文件中收到此錯誤-“#的未定義局部變量或方法'amount'”我在下面列出我的特定github以及以下相關內容。 非常感謝您的幫助:)
確切的錯誤消息:
未初始化的常量Charge :: Amount app / models / charge.rb:18:在receipt' app/controllers/charges_controller.rb:64:in
塊中(2個級別)在show'app / controllers / charges_controller.rb:60:in`節目'
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
您的模型中有一個資本Amount
,該資本Amount
將引用一個常數。 它應該是小寫amount
使用屬性上的電荷記錄,不要。
我認為應該是: ["Amount", "$#{amount / 100}.00"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.