如何在rails7中使用format js?
How to use format js in rails 7?
我在 rails 7 中使用 gem 条带,我想访问结帐页面以进行付款或取消订单,但是,因为它是 rails 7(并且没有webpacker和javascripts相关的东西)它不想在请求时识别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
这是识别请求中的 format.js 的代码 /chekout/create。js.erb
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"
Link访问条纹支付方式
<%= 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 网站以完成交易。 Here is a youtube link where explain how to set up in Rails 7 and I found the solution here - 希望对你有用
我在 rails 7 中使用 gem 条带,我想访问结帐页面以进行付款或取消订单,但是,因为它是 rails 7(并且没有webpacker和javascripts相关的东西)它不想在请求时识别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
这是识别请求中的 format.js 的代码 /chekout/create。js.erb
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"
Link访问条纹支付方式
<%= 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 网站以完成交易。 Here is a youtube link where explain how to set up in Rails 7 and I found the solution here - 希望对你有用