我在rails 7中使用gem stripe,我想访问结帐页面进行付款或取消订单,然而,由于它是rails 7(并且没有webpacker和与javascript无关),它不想在请求时识别js格式
我checkout_controller.rb
class CheckoutController < ApplicationController def create @session = Stripe::Checkout::Session.create({ success_url: root_url, cancel_url: root_url, payment_method_types: ['card'], line_items: [ { price: 'price_1JLaadjkjcanlsr', quantity: 1 }, ], mode: 'payment', }) # here is my problem, not working respond_to do |format| format.js end end end
-
如果它能识别request/checkout/create.js.erb
中的format.js代码var stripe = Stripe("<%= Rails.application.credentials.dig(:public_key) %>"); stripe.redirectToCheckout({ sessionId: '<%= @session.id %>' }).then(function (result) { console.log(result.error.message); });
-
路线
post "checkout/create", to: "checkout#create", as: "checkout_create"
-
分条支付方式链接
<%= button_to 'Checkout', checkout_create_path, method: :post, remote: true %>
-
我的条带脚本在application.html.erb
<script src="https://js.stripe.com/v3/"></script> <%= stylesheet_link_tag "inter-font", "data-turbo-track": "reload" %>
我得到它的误差
# ActionController::UnknownFormat Extracted source (around line #21):
})
respond_to do |format|
format.js
end
end
如果有人能够在请求中使用格式js在rails 7上工作,并且可以帮助我,我将非常感激
class CheckoutController < ApplicationController
def create
@session = Stripe::Checkout::Session.create({
success_url: root_url,
cancel_url: root_url,
payment_method_types: ['card'],
line_items: [
{ price: 'price_1JLaadjkjcanlsr', quantity: 1 },
],
mode: 'payment',
})
# Try do this! Hope this work for you
redirect_to @session.url, allow_other_host: true
end
当您使用Stripe Checkout时,您需要重定向到Stripe站点以完成交易。这里有一个youtube链接,解释了如何在Rails 7中设置,我在这里找到了解决方案-希望它对你有用