Subscription form with Stripe not passing parameters properly

北慕城南 提交于 2019-12-12 06:09:15

问题


I am getting the following error on my Rails 4.2 application. I'm trying to setup subscriptions with Stripe. A subscription belongs to a business and has_one plan.

On my view I pass the params in the URL: http://localhost:3000/subscriptions/new?plan_id=2&business_id=1001

After I submit the form I get the error below and my code follows. Forgive me if this is a beginner question.

Subscriptions Controller

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

Subscription Model

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

Subscription View (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 %>

Plan Model

class Plan < ActiveRecord::Base

  belongs_to :subscription

end

回答1:


Calling render only loads the view for an action, it doesn't run any of the logic in the method behind the action, which is why there is no @plan available when you render :new from the create action.




回答2:


I figured out the issue was with my association between Plans and Subscriptions. I had Plans belongs_to Subscription when I should have had it the other way around.

class Subscription < ActiveRecord::Base

  belongs_to :business
  belongs_to :plan
  ...


来源:https://stackoverflow.com/questions/31350606/subscription-form-with-stripe-not-passing-parameters-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!