我有一個Rails應用程序,其中Stripe設置類似於this文檔。當我打的「支付用卡」按鈕,進入它說,它是成功的測試卡信息,然後給了我一個錯誤消息說:Rails:條紋似乎工作,但不收費
No route matches [POST] "/charges/new"
即使我的路線是這樣的:
resources :charges
get 'charges/shipping'
get 'charges/address'
post 'charges/update_order'
我charges_controller
讀這樣的:
class ChargesController < ApplicationController
...
def new
@order = current_order
end
def create
# Amount in cents
@amount = current_order.total * 100
@user = current_user
@order = current_order
if @user.stripe_customer_id
customer = @user.stripe_customer_id
else
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
@user.update_attributes(stripe_customer_id: customer.id)
end
@order.update_attributes(order_status_id: 2)
@order.update_attributes(date_placed: DateTime.now)
# OrderMailer.order_placed(@order.user, @order).deliver_now
session[:order_id] = nil
charge = Stripe::Charge.create(
:customer => @user.stripe_customer_id,
:amount => @amount.to_i,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
private
def order_params
params.permit(:subtotal, :tax, :shipping, :total, :order_status_id, :user_id, :date_placed, :shipping_choice)
end
end
如果我手動插入post "charges/new"
到它說,這是成功的routes.rb
,但它重定向到同一頁面(指示Stripe::CardError
),並且在條形儀表板中不收費。
任何人都可以看到我要去哪裏錯了嗎?
你能告訴我們'#create'方法的結束嗎?我們看到'rescue'塊的結尾,但不是'#create'方法的結尾。它應該在成功後重定向。此外,如果出現錯誤,您應該渲染:new'而不是'redirect_to new_charge_path',這將刪除所有表單值。 – Chloe