0

我在付费广告工作板上工作,并且正在使用#Payola-Payments(使用 Rails 应用程序实现Stripe 支付处理器)对应用程序上的每个工作职位收费。

这就是我喜欢做的事情:

选中复选框时,我希望应用程序扣除的价格从我的工作表中的默认设置价格更改为添加复选框的价值。

我喜欢做的事的图解说明:

选中此框后,假设我在作业表中的默认价格上增加了 20 美元。

我的职位发布表

架构.rb

注意:在我的工作表中设置了 200 美元的默认价格(即 20000 美分)。原因是 Payola-payments 文档中的要求。因此,任何时候任何人发布招聘广告,Stripe 都会从他/她的信用卡中扣除 200 美元。

ActiveRecord::Schema.define(version: 20160827065822) do    
 create_table "jobs", force: :cascade do |t|
  t.string   "title",            limit: 255
  t.string   "category",         limit: 255
  t.string   "location",         limit: 255
  t.text     "description"
  t.text     "to_apply"
  t.datetime "created_at",                                   null: false
  t.datetime "updated_at",                                   null: false
  t.string   "name",             limit: 255
  t.integer  "price",                        default: 20000
  t.string   "permalink",        limit: 255
  t.string   "stripeEmail",      limit: 255
  t.string   "payola_sale_guid", limit: 255
  t.string   "email",            limit: 255
  t.string   "website",          limit: 255
  t.string   "company_name",     limit: 255
  t.boolean  "highlighted"
 end
end

我为解决这个问题所做的工作:

我在我的模型(job.rb)中定义了一个调用price_modification并调用before_save它的方法,这样我的模型看起来像下面的代码但没有工作

class Job < ActiveRecord::Base

  include Payola::Sellable
  before_save :price_modification

  def price_modification
    price
  end

  def price_modification=(new_price)
    if :highlighted == t
      self.price += (price + 2000)
    else
      self.price = new_price
    end
  end

end

提前致谢。

我正在使用Ruby 2.2.4Rails 4.2.5.1

4

1 回答 1

1

price_modification是一种不做任何更改的访问器方法。

before_save :price_modification正在调用price_modification仅返回price值但不进行任何更改的方法。

我不确定你在找什么,但我最好的猜测是这样的:

class Job < ActiveRecord::Base
  ...

  # Use before_create instead of before_save so
  # apply_extra_fees is *not* called multiple times.
  before_create :apply_extra_fees

  HIGHLIGHT_FEE_IN_CENTS = 2000

  def apply_extra_fees
    if highlighted?
      self.price += HIGHLIGHT_FEE_IN_CENTS
    end
  end

  ...
end
于 2016-08-29T13:52:31.120 回答