gpt4 book ai didi

ruby-on-rails - 使用 strip 创建订阅

转载 作者:数据小太阳 更新时间:2023-10-29 07:28:39 27 4
gpt4 key购买 nike

我正在尝试使用 Stripe 创建订阅包。这是我目前所拥有的

我的 Controller 方法。

     def subscription_one
session[:tab] = "$1.99/Month"
@subscription = Subscription.where(user_id:current_user.id)
end
def create
@subscription = Subscription.new(params[:subscription])
if @subscription.save_with_payment
redirect_to @subscription, :notice => "Thank you for subscribing!"
else
render :new
end
end

subscription_one.html.erb

<% if @subscription.present? %>
CREDIT CARD DETAILS PRESENT
<% else %>
<form action="/membership/apply" method="POST" id="payment-form">
<article>
<label class="amount"> <span>Amount: $5.00</span> </label>
</article>

<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"></script>

<% end %>

在我为出现的字段提供所有值后,当我提交时出现错误

ActionController::InvalidAuthenticityToken in MembershipController#create

有什么想法吗?

最佳答案

几个问题:

--

表单

您包含的表单是硬编码的

您遇到的问题是,如 Philidor Green 所述,此表单不会有正确的 authenticity token由 Rails 提供。根据经验,Rails 为大多数 HTML 元素提供帮助器,允许您为您的应用程序创建一致的代码:

<%= form_tag path do %>
<% end %>

你应该使用 form_tag为此

--

订阅

Subscription.new(params[:subscription])

应该是:

def subscribed_one
Subscription.new(subscription_params)
end

private

def subscription_params
params.require(:subscription).permit(:params, :attributes)
end

--

更新

为了处理这个问题,我会这样做:

#view
<% if @subscription.present? %>
Credit Card Details Present
<% else %>
<%= form_tag membership_apply_path, id: "payment-form" do %>
<%= content_tag :article do %>
<%= label_tag class: "amount" %>
<%= content_tag :span, "Amount: $5.00" %>
<% end %>
<% submit_tag "Susbcribe" %>
<% end %>
<% end %>

#app/views/layouts/application.html.erb
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500">
</script>

#app/controllers/subscriptions_controller.rb
def subscription_one
session[:tab] = "$1.99/Month"
@subscription = Subscription.where(user_id:current_user.id)
end

def create
@subscription = Subscription.new(params[:subscription])
if @subscription.save_with_payment
redirect_to @subscription, :notice => "Thank you for subscribing!"
else
render :new
end
end

关于ruby-on-rails - 使用 strip 创建订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23804110/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com