5

Stripe documentation on errorsを見てきましたが、これらのエラーを適切に処理/リダイレクトするのにまだ問題があります。基本的に何が起こっても、私は彼らにeditアクション(edit_profile_path経由)に戻り、メッセージを表示します(成功したかどうかにかかわらず)。ストリーミングエラーと例外を適切に処理するRuby with one-time charge

私はeditアクションのフォームに、updateアクションへのPOSTがあります。これは、有効なクレジットカードで正しく機能しています(料金はストライプダッシュボードにあります)。私はStripe.jsを使用しています。あなたは今redirect_toにフラッシュメッセージを渡すことができますが

class ExtrasController < ApplicationController 

    def edit 
    @extras = current_user.extras 
    end 

    def update 

    Stripe.api_key = "hidden" 

    token = params[:stripeToken] 

    begin 
     charge = Stripe::Charge.create(
     :amount => 5000, # amount in cents 
     :currency => "usd", 
     :card => token, 
     :description => current_user.email 
    ) 
    rescue Stripe::CardError => e 
     # redirect_to edit_extras_path, notice: e.message 
     # What I'm trying to do, but obviously results in AbstractController::DoubleRenderError 
    rescue => e 
     # Something else happened, completely unrelated to Stripe 
     # Display a generic error message 
    end 

    redirect_to edit_extras_path, notice: "Card charged successfully." 
    end 

end 
+1

チャンスが得られたら、このロジックをモデルに移動することをお勧めします。 – tommyd456

+0

私はこれを投稿してからやっています。しかし、お勧めはありがたいです。 – gbdev

+0

いい男 - 実際に私を専用のサービスオブジェクトに移動しましたが、それはどのモデルにも適合しないと感じました。 – tommyd456

答えて

10

は、あなたも、まだ自身でフラッシュを操作することができます。

だからあなたの更新コードに若干の変更は、あなたがやりたいことができます:

def update 

    Stripe.api_key = "hidden" 

    token = params[:stripeToken] 

    begin 
    charge = Stripe::Charge.create(
     :amount => 5000, # amount in cents 
     :currency => "usd", 
     :card => token, 
     :description => current_user.email 
    ) 
    # No exceptions were raised; Set our success message. 
    flash[:notice] = 'Card charged successfully.' 
    rescue Stripe::CardError => e 
    # CardError; display an error message. 
    flash[:notice] = 'That card is presently on fire!' 
    rescue => e 
    # Some other error; display an error message. 
    flash[:notice] = 'Some error occurred.' 
    end 

    redirect_to edit_extras_path 
end 

彼らの目的はあなたのメッセージをより明確にするために、あなたはalertのエラー状態でnoticeをスワップアウトする場合がありますまたはerrorフラッシュタイプ。 CSSで簡単にスタイルを設定して、成功または失敗を一目でわかるようにすることができます。 (BootstrapFoundationなど、さまざまな種類のアラートを表示するためのスタイルが用意されています)

+0

私はあまりにも明らかにこの方法を探していた。シンプルなソリューションをありがとう。そして、はい、私はちょうど 'アラート'対 '通知'フラッシュでそれを正確にしました。乾杯! – gbdev

+0

シンプルなものを見落とすのはいつも簡単です!それが助けてくれてうれしい。 – colinm