我正在构建一个应用程序来处理不同服务的不同类型的交易,例如订阅,礼品订阅和购买。
礼品交易和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
为了使事情更加清晰,从控制器创建方法,它将用户重定向到审查操作,其中有一个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
答案 0 :(得分:0)
因此,经过几个小时的敲击,结果很简单......在输入信用卡数据的视图中,应该保存到数据库的字段没有被保存,因为我只有
一个link_to按钮,在调用credit_card方法时继续并执行ergo。感谢lander16指出这一点。