Search code examples
ruby-on-rails-4mongoidibm-cloudmlabmongoid4

How do I pass secret keys into mongoid.yml using secrets.yml in Rails 4.2?


This is my first time using Mongoid, everything works fine in test/local server, but I am hitting a wall when it comes to deploying into IBM Bluemix (Don't ask me why Bluemix, I know it is probably easier if I deploy to Heroku using Figaro.)

I am connecting to MongoLab using it's uri.

In my scerets.yml:

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
  mongo_url: "mongodb://IbmCloud_xxxxxx_xxxxxx:[email protected]:xxxxxx/IbmCloud_xxxxxx_xxxxxx"

In my mongoid.yml

production: 
sessions:
    default:
        # The standard MongoDB connection URI allows for easy replica set connection setup. 
        # Use environment variables or a config file to keep your credentials safe.
        uri: <%= Rails.application.secrets.mongo_url %>

When I deploy the app, staging fails because the uri is nil. I knew this because I tested by hardcoding the uri and it works.

I currently do not include mongoid.yml in my .gitignore, so I though if I can insert uri: <%= Rails.application.secrets.mongo_url %>, I should be safe.

I also tried hardcoding the uri into mongoid.yml and include it in gitignore, but it gives me some other bugs and I wonder which is a better approach.

Here is the error message I get:

2015-07-07T17:29:53.01-0400 [STG/0]      OUT        rake aborted!
2015-07-07T17:29:53.01-0400 [STG/0]      OUT        NoMethodError: undefined method `match' for nil:NilClass
2015-07-07T17:29:53.01-0400 [STG/0]      OUT        /tmp/staged/app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/sessions/mongo_uri.rb:49:in `initialize'
2015-07-07T17:29:53.01-0400 [STG/0]      OUT        /tmp/staged/app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/sessions/factory.rb:106:in `new'
2015-07-07T17:29:53.01-0400 [STG/0]      OUT        /tmp/staged/app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/sessions/factory.rb:106:in `parse'
2015-07-07T17:29:53.01-0400 [STG/0]      OUT        /tmp/staged/app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/sessions/factory.rb:62:in `create_session'

Again, this is because the uri I setup in secrets.yml is showing nil in mongoid.yml.

Thanks in advance!!


Solution

  • The Ruby buildpack is overriding the URL you have. You need to attach either the mongolab or mongodb service to your app. The buildpack will then auto wire in your database for you.