1

Rails アプリで何か変なことをする必要があります。ユーザーが create アクションを介して Product インスタンスを作成したら、それを保存してから、アカウントにまだ追加していない場合は Braintree 支払い方法フォームにリダイレクトする必要があります。製品。

商品の作成アクションは次のとおりです。

def create
    @product = Product.new(product_params)
    @product.set_user!(current_user)
      if @product.save
                if !current_user.braintree_customer_id?
                    redirect_to "/customer/new"
                else
                    redirect_to view_item_path(@product.id)
                end
      else
        flash.now[:alert] = "Woops, looks like something went wrong."
                format.html {render :action => "new"}
      end
  end

Braintree カスタマー コントローラーの確認メソッドは次のとおりです。

def confirm
    @result = Braintree::TransparentRedirect.confirm(request.query_string)

    if @result.success?
      current_user.braintree_customer_id = @result.customer.id
      current_user.customer_added = true
            current_user.first_name = @result.customer.first_name
            current_user.last_name = @result.customer.last_name
      current_user.save!
            redirect_to ## not sure what to put here
    elsif current_user.has_payment_info?
      current_user.with_braintree_data!
      _set_customer_edit_tr_data
      render :action => "edit"
    else
      _set_customer_new_tr_data
      render :action => "new"
    end
  end

私がやりたいことは可能ですか?

4

1 に答える 1

2

ブレインツリー フォームにリダイレクトする前に、製品 ID をセッション変数に格納できます。確認が完了したら、セッションからこの ID を読み取り、製品表示アクションにリダイレクトします。

if !current_user.braintree_customer_id?
  session[:stored_product_id] = @product.id
  redirect_to "/customer/new"
else
  redirect_to view_item_path(@product.id)
end

ユーザーが製品 ID を知っていれば、有効な URL アドレスを入力するだけで製品ビュー ページを開くことができるので、この種の状況にも対処する必要があります。製品表示アクションに before_filter を配置して、ユーザーがブレイン ツリーをセットアップしているかどうかを確認できます。このようにすれば、create アクションに条件を指定する必要はありません。いつでも製品ショー ページにリダイレクトできます。before_filter は、ユーザーがブレインツリー データを更新する必要があるかどうかを確認します。

于 2014-09-04T07:38:17.197 に答える