我正试图在我的Rails应用程序中按照以下步骤实现Stripe。文件. 我已经按照步骤到 "4保存卡的细节",其中一个表格,如。这个 应该显示。然而,当我点击 "订阅 "按钮时,什么都没有发生。
我使用的控制器叫做 "订阅"。当我的提交按钮在subscribesshow视图中时,我得到的日志错误是 ActiveRecord::RecordNotFound (Couldn't find Subscription with 'id'=StripeElements):
如果我把它移到subscriptionsindex视图中,我得到的日志错误是 ActionController::RoutingError (No route matches [GET] "/StripeElements.css"):
我还没有修改我的路由或控制器,因为文档中没有说需要这样做。我的建议是 此处 和 此处 还没有解决我的问题。
我使用Rails 5.2.4, Ruby 2.6 & Bootstrap 4.
show.html.erb
<head>
<script src="https://js.stripe.com/v3/"></script>
<link rel="stylesheet" href="StripeElements.css">
</head>
<section class="subscription mt-3">
<div class="container-fluid text-center bg-grey">
<div class="row">
<div class="col-12 justify-content-center align-item-center mb-1">
<h4><%= @subscription.name %></h4>
</div>
<body>
<form id="subscription-form">
<div id="card-element" class="MyCardElement">
<!-- Elements will create input elements here -->
</div>
<!-- We'll put the error messages in this element -->
<div id="card-errors" role="alert"></div>
<button type="submit">SUBMIT</button>
</form>
</body>
</section>
<script type="text/javascript">
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
</script>
appassetsjavascriptsscript.js。
var stripe = Stripe('pk_test_NjMKZ5dBTvfebS965Xcu4EbP00veP5dc9C');
var elements = stripe.elements();
appassetsstylesheetsMyCardElements.css。
.MyCardElement {
height: 40px;
padding: 10px 12px;
width: 100%;
color: #32325d;
background-color: white;
border: 1px solid transparent;
border-radius: 4px;
box-shadow: 0 1px 3px 0 #e6ebf1;
-webkit-transition: box-shadow 150ms ease;
transition: box-shadow 150ms ease;
}
.MyCardElement--focus {
box-shadow: 0 1px 3px 0 #cfd7df;
}
.MyCardElement--invalid {
border-color: #fa755a;
}
.MyCardElement--webkit-autofill {
background-color: #fefde5 !important;
}
appassetsjavascriptsclient.js
var style = {
base: {
color: "#32325d",
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: "antialiased",
fontSize: "16px",
"::placeholder": {
color: "#aab7c4"
}
},
invalid: {
color: "#fa755a",
iconColor: "#fa755a"
}
};
var cardElement = elements.create("card", { style: style });
cardElement.mount("#card-element");
Subscriptions_controller.rb
class SubscriptionsController < ApplicationController
before_action :set_subscription, only: [:show, :edit, :update, :destroy]
# GET /subscriptions
# GET /subscriptions.json
def index
@subscriptions = Subscription.all
end
# GET /subscriptions/1
# GET /subscriptions/1.json
def show
@subscription = Subscription.find(params[:id])
end
private
# 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.fetch(:subscription, {})
end
end
我没有按照Stripe的文档来做,而是按照这个指南来做,并且获得了成功。