我一直在嘗試訪問我在Rails加密憑證中設定的API密鑰。我使用 Vim 將密鑰添加到檔案中,并仔細檢查了它們是否正確保存。
我在 config.require_master_key = true 的測驗、開發和生產檔案中添加了 config/environments。
但是當我嘗試使用其中一個 API 密鑰時,它回傳 nil。
我的證書是這樣設定的:
我的證書是這樣設定的。
# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: 1234567890。
條紋:
開發: 開發:
stripe_public_key: pk_test_1234567890
stripe_secret_key: sk_test_1234567890
條紋:
測驗:
stripe_public_key: pk_test_1234567890
stripe_secret_key: sk_test_1234567890
條紋:
生產:
stripe_public_key: pk_live_1234567890
stripe_secret_key: sk_live_1234567890
然后在我想使用它的檔案中(new.html.erb),我試圖在一個腳本中訪問證書。
const stripe = Stripe("<%= Rails.application.credentials.stripe.stripe_public_key %>")
但它沒有回傳任何東西。
在通過 Stack Overflow 和 Rails docs 挖掘后,我嘗試了其他幾個版本/語法,但還沒有得到任何作業。
我還嘗試了 當我運行服務器,并在Chrome開發工具中查看腳本時,它只是顯示為 值得注意的是,當我運行 uj5u.com熱心網友回復: 正如@DennyMueller在評論中指出的,問題是'stripe'鍵在檔案中被覆寫了,最后只有最后一個鍵存在于配置中。
無論如何,指出這并不是 Rails 所期望的管理依賴于環境的憑證的方式。相反,你應該有單獨的檔案 因此,每個憑證檔案都不應該把環境名稱作為里面的一個關鍵。
標籤: 下一篇:將JSON轉換為完整的表格
const stripe = Stripe("<%= Rails.application.credentials.stripe[Rails.env.to_sym][:stripe_public_key] %>")/code>但也沒有成功。
const stripe = Stripe("")rails credentials:show時,憑證被正確地列印到終端,因此它們確實存在。我一定是訪問它們的方式不正確。
config/credentials/development.yml.enc 和 config/credentials/test.yml.enc,它們是通過運行rails credentials:edit --environment development
rails credentials:edit --environment test
