gpt4 book ai didi

javascript - 如何将环境变量嵌入到 Rails 中的 .coffee 文件中?

转载 作者:行者123 更新时间:2023-11-30 15:17:58 25 4
gpt4 key购买 nike

我看到了这个问题,但没有用:How to access environment variable from rails on your javascript?

我有这个代码

订阅.coffee.erb
$ ->
# Create a Stripe client
stripe = Stripe( "<% ENV['STRIPE_PUBLIC'] %>" )

我已经设置好了这个环境

C:\Users\Chloe\workspace\>echo %STRIPE_PUBLIC%
pk_test_7aMtxxxxxxxxxxxxx4p3M

C:\Users\Chloe\workspace\>rails server --bind 0.0.0.0
=> Booting Puma

然而它正在生成这个 JS:

http://localhost:3000/assets/subscriptions.self-dfceb50c7f2eec8201fd3f59a7660a6763333b1e27f564e812f93c8ce9019188.js?body=1

(function() {
$(function() {
var card, elements, form, stripe, stripeTokenHandler, style;
stripe = Stripe("");

最佳答案

在您的模板中,您缺少 = 来实际呈现 ERB 表达式的输出。以下应该有效:

stripe = Stripe("<%= ENV['STRIPE_PUBLIC'] %>")

参见 the docs了解更多。

关于javascript - 如何将环境变量嵌入到 Rails 中的 .coffee 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44207224/

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