4

我正在构建一个应用程序来处理不同服务的不同类型的交易,例如订阅、礼物订阅和购买。

我对礼品交易和 activemerchant 有疑问。我将简要概述它的工作原理。

用户创建礼物订阅并为其填写数据,将其存储在数据库中,然后在自定义“show_view”中显示给用户以供查看,然后用户继续以单独的表格输入信用卡信息,当他提交数据时,会调用控制器中的一个方法来处理事务,这就是我遇到问题的地方。

这是 gift_subscription.rb 模型

    def gift_purchase
    response = GATEWAY.purchase(price, credit_card, gift_purchase_options)
    GiftTransaction.create!(:action => "gift_purchase", :amount => price, :response => response)
    response.success?
  end
  private
  def gift_purchase_options
    {
        :ip => ip_address,
        :billing_address => {
            :name     => name + last_name,
            :address1 => address1,
            :city     => city,
            :state    => state,
            :country  => "Mexico",
            :zip      => zip
        }
    }
  end

  def validate_card
    unless credit_card.valid?
      credit_card.errors.full_messages.each do |message|
        errors[:base] << message
      end
    end
  end

  def credit_card
    @credit_card = ActiveMerchant::Billing::CreditCard.new(
        :brand              => card_type,
        :number             => card_number,
        :verification_value => card_verification,
        :month              => card_expires_on.month,
        :year               => card_expires_on.year,
        :first_name         => name,
        :last_name          => last_name
    )

这是gift_subscription_controller.rb

def review
    @gift_subscription = GiftSubscription.find(params[:id])
  end

  def edit_review
    @gift_subscription = GiftSubscription.find(params[:id])
  end

  def update_review
  @gift_subscription = GiftSubscription.find(params[:id])

    respond_to do |format|
      if @gift_subscription.update_attributes(params[:gift_subscription])
        format.html { redirect_to "gift_subscriptions/review/#{@gift_subscription.id}", :notice => 'Gift subscription was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit_review" }
        format.json { render :json => @gift_subscription.errors, :status => :unprocessable_entity }
      end
    end
  end

  def do_gift_transaction
    @gift_subscription = GiftSubscription.find(params[:id])
    if @gift_subscription.gift_purchase
        redirect_to '/thank_you'
    else
        redirect_to "/gift_subscriptions/#{@gift_subscription.id}/failed_transaction"
    end
  end

  def failed_transaction
    @gift_subscription = GiftSubscription.find(params[:id])
    @gift_transactions = @gift_subscription.gift_transactions
  end

  def transaction_details
    @gift_subscription = GiftSubscription.find(params[:id])
  end

为了让事情更清楚一点,从控制器的 create 方法中,它会将用户重定向到评论操作,其中有一个 edit_review 以防他们想要更改某些内容,然后他们转到 transaction_details 输入信用卡信息的地方,最后调用方法 do_gift_transaction实际进行交易。

我得到的错误如下

NoMethodError in GiftSubscriptionsController#do_gift_transaction

undefined method `month' for nil:NilClass
Rails.root: /home/peanut/RubymineProjects/GiftBox

Application Trace | Framework Trace | Full Trace
app/models/gift_subscription.rb:44:in `credit_card'
app/models/gift_subscription.rb:12:in `gift_purchase'
app/controllers/gift_subscriptions_controller.rb:113:in `do_gift_transaction'

我一直在环顾四周,似乎无法找到为什么它不能识别月份...对于其他订阅,我的模型基本相同(有一些差异),但效果很好。在这里的任何帮助将不胜感激。

GiftSubscription 模型属性

attr_accessible :response, :name, :last_name, :address1, :address2,:city,
                  :state, :zip, :card_type, :ip_address, :price,
                  :duration, :created_at, :card_expires_on, :card_number,
                  :card_verification, :message

  has_one :gift_transactions, :class_name => "GiftTransaction"
  attr_accessor :card_number, :card_verification
  validate :validate_card, :on => :transaction_details
4

1 回答 1

0

因此,经过几个小时的敲打,结果证明这很简单......在输入信用卡数据的视图中,应该保存到数据库的字段没有被保存,因为我只有一个 link_to 按钮在调用 credit_card 方法时继续并因此,它是空的。

感谢lander16 指出这一点。

于 2012-10-19T01:52:27.587 回答