RailsApps / rails-stripe-membership-saas

An example Rails 4.2 app with Stripe and the Payola gem for a membership or subscription site.
http://railsapps.github.io/rails-stripe-membership-saas
1.14k stars 232 forks source link

I get "KeyError: key not found" . #143

Open real-ashwin opened 9 years ago

real-ashwin commented 9 years ago

I am not an expert in rails, but here's the stack trace.

It occurs just after "run bundle exec rake db:migrate from "."" and happens for all future "rake" commands.

rake
rake aborted!
KeyError: key not found
(erb):18:in `fetch'
(erb):18:in `<main>'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/application.rb:391:in `secrets'
/Users/ashwin/Documents/workspace/rails-starter/rails-stripe-membership-saas/config/initializers/devise.rb:15:in `block in <top (required)>'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/devise-3.5.1/lib/devise.rb:281:in `setup'
/Users/ashwin/Documents/workspace/rails-starter/rails-stripe-membership-saas/config/initializers/devise.rb:3:in `<top (required)>'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/engine.rb:652:in `block in load_config_initializer'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/notifications.rb:166:in `instrument'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/engine.rb:651:in `load_config_initializer'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/engine.rb:615:in `each'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/engine.rb:615:in `block in <class:Engine>'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/initializable.rb:30:in `instance_exec'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/initializable.rb:30:in `run'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/initializable.rb:55:in `block in run_initializers'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/initializable.rb:44:in `each'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/initializable.rb:44:in `tsort_each_child'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/initializable.rb:54:in `run_initializers'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/application.rb:352:in `initialize!'
/Users/ashwin/Documents/workspace/rails-starter/rails-stripe-membership-saas/config/environment.rb:5:in `<top (required)>'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/ashwin/Documents/workspace/rails-starter/rails-stripe-membership-saas/test/test_helper.rb:2:in `<top (required)>'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/ashwin/Documents/workspace/rails-starter/rails-stripe-membership-saas/test/models/user_test.rb:1:in `<top (required)>'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/Users/ashwin/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.2.1/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run
(See full trace by running task with --trace)
DanielKehoe commented 9 years ago

You need to set the MAILCHIMP_API_KEY and STRIPE_API_KEY values in either your Unix ENV variables or the config/secrets.yml file.

real-ashwin commented 9 years ago

Hey Daniel, Thanks for the quick reply but unfortunately that did not fix it :( ..

rsgrafx commented 9 years ago

Open terminal and at the command line $> source ~/.bash_profile or ~/.profile. at the command prompt and type `echo $MAILCHIMP_API_KEY - or any of the keys in question. if you see the key you specified. restart rails.

real-ashwin commented 9 years ago

I can see the key but the error still exists . On May 29, 2015 12:09 AM, "Orion Engleton" notifications@github.com wrote:

Open terminal and at the command line $> source ~/.bash_profile or ~/.profile. at the command prompt and type `echo $MAILCHIMP_API_KEY if you see the key you specified. restart rails.

— Reply to this email directly or view it on GitHub https://github.com/RailsApps/rails-stripe-membership-saas/issues/143#issuecomment-106718497 .

rsgrafx commented 9 years ago

I'm getting the same error cant find the issue myself .. this is after doing a search and commenting out all blocks of code where I see Rails.application.secrets.whatever_key , in the initializers - in the user model in the secrets.yml. Just to get the first initial migration going.

$> rake db:migrate

rake aborted!

KeyError: key not found: "MAILCHIMP_API_KEY" (erb):20:in `fetch' (erb):20:in

DanielKehoe commented 9 years ago

In your config/secrets.yml file, replace the following:

  mailchimp_api_key: <%= ENV.fetch("MAILCHIMP_API_KEY") %>
  mailchimp_list_id: <%= ENV.fetch("MAILCHIMP_LIST_ID") %>
  stripe_api_key: <%= ENV.fetch("STRIPE_API_KEY") %>
  stripe_publishable_key: <%= ENV.fetch("STRIPE_PUBLISHABLE_KEY") %>

with:

  mailchimp_api_key: yourkey
  mailchimp_list_id: yourlistid
  stripe_api_key: yourkey
  stripe_publishable_key: yourkey

If you are having trouble with Unix ENV variables you don't have to use them. Just put the values into your config/secrets.yml file. But don't check it into git, just diagnose the problem by hardcoding the values.

real-ashwin commented 9 years ago

@rsgrafx Did you manage to fix this issue? @DanielKehoe Can we 'open' this issue?

DanielKehoe commented 9 years ago

I've reopened the issue as requested. Is there anyone who has not been able to resolve the issue by setting the Unix ENV variables or hardcoding the vales in the config/secrets.yml file?

nikolay12 commented 9 years ago

I haven't been able, either. I changed quite a few things and made it to work. Will commit to the fork and than try to diff to find out what is it that made it work.

==EDIT: just pushed my changes to my fork - you can have a look at the differences - it should be in the config directory. I've excluded the files containing sensitive data (the *yml ones) - I can give you a censored version of them, too - if necessary.

DanielKehoe commented 9 years ago

The only purpose of the fetch method is to force an error if the API keys are not available in the Unix ENV variables when the application is started. This lets the developer know that the API key is not available and avoids more mysterious errors later. You can remove the fetch method from the config/secrets.yml file and the application will run without the initial error. But it will fail later if the API key is not available.

See https://github.com/RailsApps/rails-stripe-membership-saas/commit/3fce46406f9786109cf3bcd536c67e71fe1caae4

real-ashwin commented 9 years ago

@DanielKehoe More info : I get this error while running the composer command $ rails new rails-stripe-membership-saas -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb

The errors don't go away even after adding the API keys in the environment variables.

DanielKehoe commented 9 years ago

@ashwinputhige Could you clarify please, do you get the I get "KeyError: key not found" when the Rails Composer script is running (causing a failure before an app is created)? Or do you build an app using Rails Composer successfully, and then get the error when you launch the app with rails server?

real-ashwin commented 9 years ago

I get it when the rails composer script is running before the app is created. It happens at the command bundle exec rake db:command (I believe its db:migrate). I can retry if that is important.

DanielKehoe commented 9 years ago

Thank you @ashwinputhige, that is helpful.

DanielKehoe commented 9 years ago

I've released a new version of Rails Composer that removes the fetch method from the config/secrets.yml file. @ashwinputhige please give it a try and let me know if the issue is resolved.

kathyonu commented 9 years ago

@ashwinputhige : This note replaced with my next comment.

kathyonu commented 9 years ago

@ashwinputhige @nikolay12 @rsgrafx : Please see PR Testsformaster #150 . I invite you to pull this branch into your working app, and take it for a spin. You will see it has tests that resolve Issue #143 . If you can help resolve the four pending tests, that would be grand, thank you.

real-ashwin commented 9 years ago

@kathyonu Sorry, I am not using this application and so I will not be able to run your tests. Please feel free to close the issue if required.

nikolay12 commented 9 years ago

I just git cloned the repository - the problem still persists. I cancelled my Railsapp subscription - sorry Daniel, it just does not make sense to continue paying for something that is so brittle.

kathyonu commented 9 years ago

@nikolay12 @ashwinputhige @rsgrafx @DanielKehoe Greetings .. I have changed my mind, ashwinputhige, and nikolay12 I ask you to change yours too .. If you will look at Issue #152, specifically the posts i made there yesterday ..you will find a resource that really is priceless in these matters of setting the enviroment variables. So that you know, I have an in production rails-stripe-membership-saas running sweet as can be, and I have gone through the exact same issues you are facing and I can easily help resolve them for you .. and others you may run into. Please re-signup with RailsApps if need be and accept this offer of help and you will have a high power fully-tested app at your fingertips .. the tests are arriving as referred to in Issue #152 and more to come. We are also going to be adding capabilities after what exists is fully tested.

ysksn commented 9 years ago

try this :) bin/spring stop

UbuntuEvangelist commented 5 years ago

screenshot from 2019-01-31 19-33-33 same problem using ubuntu 16

aliwaqar981 commented 4 years ago

Getting same error. Any solution?

pmutale commented 3 years ago

Here too! This was working until I updated rails to 6.1