在我的捐款模型中,我已经将amount_cents列货币化了
我需要人道版本的捐款金额(例如。643.50)在模型中,这样我就可以生成并发送一个PDF收据给捐献者。
我尝试过humanized_money(self.amount)和self.amount.humanized_money,但是得到了错误:
NoMethodError: undefined method `humanized_money' for #<Donation:0x007fa687ebb678>我怎样才能在模型中得到这种人性化的形式?
class Donation < ActiveRecord::Base
belongs_to :donatable, polymorphic: true
belongs_to :added_by_user, foreign_key: "added_by", class_name: "User"
store_accessor :details, :method
monetize :amount_cents
def donations_this_week(branch_id)
sum = Donation.sum(:amount_cents).to_money
return humanized_money(sum)
end
def receipt
Receipts::Receipt.new(
id: id,
product: "GoRails",
message: "This receipt is to acknowledge that we have received a donation with the below details from #{self.donatable.name}",
company: {
name: self.donatable.branch.organization.name,
address: "#{self.donatable.branch.address_line_1}\n#{self.donatable.branch.address_line_2}\n#{self.donatable.branch.email}\n#{self.donatable.branch.legal_details}",
email: self.donatable.branch.email,
logo: self.donatable.branch.organization.logo.url(:medium),
},
line_items: [
["Date", created_at.to_s],
["Donor Name", self.donatable.name],
["Amount", humanized_money(self.amount)],
["Payment Method", self.method],
]
)
end
end以下是数据库模式:
create_table "donations", force: :cascade do |t|
t.string "donatable_type"
t.integer "donatable_id"
t.integer "amount_cents"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "date"
t.integer "added_by"
t.jsonb "details", default: {}, null: false发布于 2018-03-14 18:57:34
不需要在模型中包括助手。
price_options = {
:no_cents_if_whole => MoneyRails::Configuration.no_cents_if_whole.nil? ? true : MoneyRails::Configuration.no_cents_if_whole,
:symbol => true
}
your_money_column.format(price_options)发布于 2016-01-08 20:01:03
在不加载ActionView::Base中的所有内容的情况下执行此操作。你可以直接做
return ActionController::Base.helpers.humanized_money sum发布于 2017-09-16 00:40:44
包括ActionView::Base将为您的模型添加许多额外的内容,不要这样做。
取而代之的是这样做:
include MoneyRails::ActionViewExtension这样,您就可以获得所有的money视图助手方法(在本文发布时为5)。
https://stackoverflow.com/questions/32086603
复制相似问题