我正在努力将Stripe与我的Rails应用程序集成。根据{{3}},我已将以下内容添加到' config / initializers / stripe.rb',并将环境变量设置为测试键:
Rails.configuration.stripe = {
:publishable_key => ENV['PUBLISHABLE_KEY'],
:secret_key => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
(根据建议编辑添加,我将其更改为以下内容)
Rails.configuration.stripe = {
:publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'] ||= Rails.application.secrets.stripe_publishable_key,
:secret_key => ENV['STRIPE_SECRET_KEY'] ||= Rails.application.secrets.stripe_secret_key,
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
当我现在尝试做任何事情(生成一个控制器,比如说,或者运行服务器)时,我得到一个错误,说我有一个"未定义的方法`secret_key'":
$ rails s
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/stripe-rails-0.3.1/lib/stripe/engine.rb:20:in `block in <class:Engine>': undefined method `secret_key' for #<Hash:0x000000066aa1e8> (NoMethodError)
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `instance_exec'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `run'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `call'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/initializable.rb:54:in `run_initializers'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/application.rb:352:in `initialize!'
from /home/ubuntu/workspace/junkyardstorefinal/config/environment.rb:5:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /home/ubuntu/workspace/junkyardstorefinal/config.ru:3:in `block in <main>'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/builder.rb:55:in `instance_eval'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/builder.rb:55:in `initialize'
from /home/ubuntu/workspace/junkyardstorefinal/config.ru:in `new'
from /home/ubuntu/workspace/junkyardstorefinal/config.ru:in `<main>'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/builder.rb:49:in `eval'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/builder.rb:49:in `new_from_string'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/builder.rb:40:in `parse_file'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/server.rb:299:in `build_app_and_options_from_config'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/server.rb:208:in `app'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/server.rb:61:in `app'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/server.rb:336:in `wrapped_app'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/server.rb:139:in `log_to_stdout'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/server.rb:78:in `start'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:80:in `block in server'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `tap'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `server'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from /home/ubuntu/workspace/junkyardstorefinal/bin/rails:8:in `require'
from /home/ubuntu/workspace/junkyardstorefinal/bin/rails:8:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `load'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `call'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/client/command.rb:7:in `call'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/client.rb:26:in `run'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/bin/spring:48:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `load'
from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `<top (required)>'
from /home/ubuntu/workspace/junkyardstorefinal/bin/spring:13:in `require'
from /home/ubuntu/workspace/junkyardstorefinal/bin/spring:13:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'
哪些其他数据对任何可以提供帮助的人都有帮助?这是整个stripe.rb那里,我无法找到我曾经使用过的其他地方&#34; secret_key&#34;或Stripe's documentation中出现的任何其他地方。我也没有触及完整描述中提到的任何文件。
奇怪的是,虽然我无法在Cloud9上启动服务器,the tutorial I was originally working through - 除了连接到Redis的问题,阻止它显示用户的购物车。
(更新添加:现在我已经合并了我所在的分支并尝试推送合并的主分支,这意味着Heroku现在编译它,我发现我也无法推送到Heroku来自主分支而没有得到&#34; NoMethodError:未定义的方法`secret_key&#39;用于#&#34;和&#34;推弃被拒绝&#34 ;.)
答案 0 :(得分:3)
我认为您的错误是ENV['PUBLISHABLE_KEY']
,而ENV['SECRET_KET']
是nil。
解决您的问题。在终端
$export PUBLISHABLE_KEY=yourpublisablekey
$export SECRET_KEY=yoursecretkey
$rails s
我想鼓励您将密钥放在secret.yml
中
development:
stripe_publishable_key: yourpublisablekey
stripe_secret_key: yoursecretkey
production:
stripe_publishable_key: yourpublisablekey
stripe_secret_key: yoursecretkey
您可以通过简单地添加Rails.application.secrets.stripe_publishable_key
和Rails.application.secrets.stripe_secret_key
例如,在config/initializers/stripe.rb
Rails.configuration.stripe = {
:publishable_key => ENV['PUBLISHABLE_KEY'] ||= Rails.application.secrets.stripe_publishable_key,
:secret_key => ENV['SECRET_KEY'] ||= Rails.application.secrets.stripe_secret_key
}
希望它有所帮助。
答案 1 :(得分:0)
事实证明,解决方案是完全取出stripe.rb的最后一行。我在CodeNewbie的某个人的建议下对此进行了评论,以确保它实际上是在设置我的条带配置。令人惊讶的是,一旦我评论出来( &#39; Stripe.api_key = Rails.configuration.stripe [:secret_key]&#39;),一切都开始完美。
答案 2 :(得分:0)
几年后,我使用Rails 5.2.1遇到了与条形导轨类似的问题
解决方案是按照当前的rails标准将密钥添加到凭据中,而不是使用初始化文件夹“ stripe.rb”,而是使用envirnoments文件夹中的development.rb
和production.rb
文件。
因此,在我的development.rb
上我有这样的东西:
Rails.configuration.stripe.secret_key = Rails.application.credentials.stripe[:dev_secret_key]
Rails.configuration.stripe.publishable_key = Rails.application.credentials.stripe[:dev_publishable_key]
还有production.rb