【问题标题】:Subscription form with Stripe not passing parameters properly带有 Stripe 的订阅表单未正确传递参数
【发布时间】:2015-07-10 21:32:56
【问题描述】:

我的 Rails 4.2 应用程序出现以下错误。我正在尝试使用 Stripe 设置订阅。订阅属于企业和 has_one 计划。

在我看来,我在 URL 中传递了参数: http://localhost:3000/subscriptions/new?plan_id=2&business_id=1001

提交表单后,我收到以下错误,我的代码如下。如果这是一个初学者问题,请原谅我。

订阅控制器

class SubscriptionsController < ApplicationController
  before_action :set_subscription, only: [:show, :edit, :update, :destroy]

  # GET /subscriptions
  def index
    @subscriptions = Subscription.all
  end

  # GET /subscriptions/1
  def show
  end

  # GET /subscriptions/new
  def new
    @subscription = Subscription.new
    @plan = Plan.find_by id: params["plan_id"]
    @business = Business.find_by id: params["business_id"]
  end

  # POST /subscriptions
  def create
    @subscription = Subscription.new subscription_params.merge(email: stripe_params["stripeEmail"],
                                                               card_token: stripe_params["stripeToken"])
    raise "Please, check subscription errors" unless @subscription.valid?
    @subscription.process_payment
    @subscription.save
    redirect_to @subscription, notice: 'Subscription was successfully created.'
  rescue => e
    flash[:error] = e.message
    render :new
  end

  private
    def stripe_params
      params.permit :stripeEmail, :stripeToken
    end
    # Use callbacks to share common setup or constraints between actions.
    def set_subscription
      @subscription = Subscription.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def subscription_params
      params.require(:subscription).permit(:plan_id, :business_id)
    end
end

订阅模式

class Subscription < ActiveRecord::Base

  belongs_to :business
  has_one :plan

  def process_payment
    customer = Stripe::Customer.create email: email,
                                       card: card_token

    Stripe::Charge.create customer: customer.id,
                          amount: plan.price * 100,
                          description: plan.name,
                          currency: 'usd'

  end

end

订阅视图 (new.html.erb)

<%= form_for @subscription do |f| %>
  <% if @subscription.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@subscription.errors.count, "error") %>
        prohibited this subscription from being saved:
      </h2>
      <ul>
        <% @subscription.errors.full_messages.each do |message| %>
          <li>
            <%= message %>
          </li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <h1><%= @business.name %></h1>
  <div class="field">
    <%= f.hidden_field :plan_id, value: @plan.id %>
  </div>
  <div class="field">
    <%= f.hidden_field :business_id, value: @business.id %>
  </div>
  <div class="actions">
    <script
      src="https://checkout.stripe.com/checkout.js" class="stripe-button"
      data-key="<%= Rails.application.secrets.stripe_publishable_key %>"
      data-image="/img/documentation/checkout/marketplace.png"
      data-name="Business Name"
      data-description="<%= @plan.name %>"
      data-amount="<%= @plan.price*100 %>">
    </script>
  </div>
<% end %>

计划模型

class Plan < ActiveRecord::Base

  belongs_to :subscription

end

【问题讨论】:

  • 首先将@plan = Plan.find_by id: params["plan_id"] 更改为@plan = Plan.find(params["plan_id"]),但您需要使其更加健壮。 IMO 如果用户请求一个不存在的计划,你需要比堆栈跟踪更优雅的东西。

标签: ruby-on-rails associations stripe-payments


【解决方案1】:

调用render 只加载一个动作的视图,它不会运行该动作背后的方法中的任何逻辑,这就是为什么当你从@987654323 @ 987654323@ 时没有可用的@plan 来自@987654324 @动作。

【讨论】:

  • 我应该改用什么,redirect_to?
【解决方案2】:

我发现问题出在计划和订阅之间的关联上。我有 Plans belongs_to 订阅,而我本应该有它。

class Subscription < ActiveRecord::Base

  belongs_to :business
  belongs_to :plan
  ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-24
    • 2019-02-15
    • 2014-11-24
    • 2012-07-30
    • 1970-01-01
    • 2022-01-25
    • 2022-11-02
    • 1970-01-01
    相关资源
    最近更新 更多