MongoDB error with Mongoid, Heroku, Device, MongoHQ and Rails - ruby-on-rails-3

I'm working on a simple app that is currently deployed to heroku now - it is using mongoid, device and can create user login very easily in localhost, but not in production at heroku. You can try to create an account, and it gives an error after submit.
I think my mongoid.yml for production needs to change, but I have no idea how.
My heroku config has the following:
=== Config Vars for bookfoo
DATABASE_URL: postgres://jeibucpexp:bVZzGaGPUeGylwmA7dyE#ec2-107-20-186-97.compute-1.amazonaws.com/jeibucpexp
GEM_PATH: vendor/bundle/ruby/1.9.1
LANG: en_US.UTF-8
MONGOHQ_URL: mongodb://heroku:14bc6d476e6449c952d6350b07e89643#alex.mongohq.com:10065/app6153931
MONGOLAB_URI: mongodb://heroku_app6153931:og03dk0hdrj7r8lsit51k6ah9n#ds037097-a.mongolab.com:37097/heroku_app6153931
PATH: bin:vendor/bundle/ruby/1.9.1/bin:/usr/local/bin:/usr/bin:/bin
RACK_ENV: production
RAILS_ENV: production
SHARED_DATABASE_URL: postgres://jeibucpexp:bVZzGaGPUeGylwmA7dyE#ec2-107-20-186-97.compute-1.amazonaws.com/jeibucpexp
mongoid.yml has the following as suggested in heroku:
development:
sessions:
default:
database: bookfoo_app_development
hosts:
- localhost:27017
options:
consistency: :strong
options:
test:
sessions:
default:
database: bookfoo_app_test
hosts:
- localhost:27017
options:
consistency: :strong
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
options:
skip_version_check: true
safe: true
my gemfile has the following:
source 'https://rubygems.org'
gem 'rails', '3.2.3'
group :development, :test do
gem 'sqlite3'
gem 'rspec-rails'
end
group :test do
gem 'database_cleaner'
gem 'mongoid-rspec'
gem 'factory_girl_rails'
gem 'email_spec'
gem 'capybara'
gem 'launchy'
end
group :production do
gem 'thin'
end
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem "mongoid", :git => "git://github.com/mongoid/mongoid.git"
gem "bson_ext"
gem "devise"
after git push heroku master it gives me the following hints:
There is a configuration error with the current mongoid.yml.
Problem:
No database provided for session configuration: :options.
Summary:
Each session configuration must provide a database so Mongoid knows where the default database to persist to. What was provided was: {"skip_version_check"=>true, "safe"=>true}.
Resolution:
If configuring via a mongoid.yml, ensure that within your :options section a :database value for the session's default database is defined.
Example:
\_\_development:
\_\_\_\_sessions:
\_\_\_\_\_\_options:
\_\_\_\_\_\_\_\_database: my_app_db
\_\_\_\_\_\_\_\_hosts:
\_\_\_\_\_\_\_\_\_\_- localhost:27017

mongoid.yml should be so (my indentation was wrong):
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
options:
skip_version_check: true
safe: true

Related

rails 5.1.4 console in production doesn't find models

I think I got a configuration issue with my rails setup in production.
I've got a puma working fine without any issues (connecting to db, loading models etc) and previously was able to use the console (I've encountered this [bug][https://github.com/rails/rails/issues/19256] but worked around it by filling in the database.yml)
When I try to open the console to perform little operations, none of my model classes are found.
✗ bundle exec rails console production
Running via Spring preloader in process 18313
Loading production environment (Rails 5.1.4)
irb(main):001:0> User.count
NameError: uninitialized constant User
from (irb):1
irb(main):002:0>
Similar questions on SO recommend to run ActiveRecord::Base.subclasses and eventually Rails.application.eager_load!
When I do try to run Rails.application.eager_load! I get uninitializaed constant error
irb(main):005:0> Rails.application.eager_load!
NameError: uninitialized constant ApplicationController
Did you mean? ApplicationCable
from app/controllers/accounts_controller.rb:1:in `<top (required)>'
from (irb):5
So I guess my console is now somehow lost and hasn't loaded the project source correctly.
How can I troubleshoot this situation ?
edit 2018-02-15 : I connected my workstation to the prod environment to use the console. This worked at first, the console was behaving correctly. After a couple times the same issues appeared. After doing spring stop I was able again to user my local workstation to open a console in prod.
Interesting fact : this never happens in dev environment, although spring had an app running in this environment.
It turns out spring is also running in prod, although the spring gem is only in dev group. (see below)
Gemfile
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
gem 'rails', '~> 5.1.4'
gem 'puma', '~> 3.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'therubyracer', platforms: :ruby
gem 'coffee-rails', '~> 4.2'
gem 'jbuilder', '~> 2.5'
group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
gem 'rails-erd', require: false, group: :development
end
group :development do
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'pry'
gem 'pry-byebug'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'devise'
gem 'devise-i18n'
gem 'cancancan', '~> 2.0'
gem 'rolify'
gem 'redis', '~> 3.2'
group :production do
gem "sidekiq"
gem "sentry-raven"
end
gem 'pg'
gem "paperclip", "~> 5.0.0"
gem 'i18n-country-translations'
gem 'rails-i18n', '~> 5.0.0'
gem 'i18n_alchemy'
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
If you deployed through Docker make sure using -ti prefix in running command like:
docker exec -ti 4d318d505e9f rails c production

How to I switch my sinatra server between development and testing?

I have a sinatra server, and according to the gemfile, there are different groups of gems. Specifically, there is an application group, a testing group, and a development group. How do I switch my application between these three groups? Specifically, I want to be running my server in testing mode so I can do TDD.
Here is the gemfile.
source 'https://rubygems.org'
ruby '2.0.0'
gem 'sinatra'
gem 'sinatra-contrib', require: %w(sinatra/config_file)
gem 'thin'
gem 'slim'
gem 'sinatra-assetpack'
gem 'zurb-foundation'
gem 'compass'
group :application do
# Here should be all the specifics for the application
end
group :test do
gem 'rspec'
gem 'coveralls'
gem 'capybara'
gem 'rubocop'
end
group :development do
gem 'rake'
gem 'guard', '2.2.3'
gem 'guard-rspec'
gem 'guard-rubocop'
gem 'guard-livereload'
gem 'guard-shotgun', git: 'git#github.com:rchampourlier/guard-shotgun.git', branch: 'master'
gem 'blam'
end
Gem Groups
Bundler installs by default everything. What you probably want is something like this:
$ bundle install --without test development
for your productive server, this install everything except the test & development group.
source: http://bundler.io/v1.3/groups.html
Sinatra Environment
Since you mentions sinatra explicit you maybe also interested in the sinatra config blocks
# run only in development environment
configure :development do
use Rack::Session::Pool, :key => '', :expire_after => 60 * 10
end
# run only in test environment
configure :test do
use Rack::Session::Pool, :key => 'session', :expire_after => 60 * 60
end
# run everytime
configure do
MongoMapper.database = 'food'
User.ensure_index(:username)
Product.ensure_index(:rnd)
end
You can set the different environments with RACK_ENV.
example:
RACK_ENV=test ruby mytest.rb
source: http://www.sinatrarb.com/configuration.html#built-in-settings

Enki deployment to Heroku - problems with assets precompile

I cloned this https://github.com/xaviershay/enki and did the following modifications:
My Gemfile now looks like this:
source 'https://rubygems.org'
gem 'rails', '3.2.6'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
end
group :production do
gem 'pg'
end
group :development do
gem 'mysql2', '~> 0.3.11'
end
platforms :jruby do
gem 'activerecord-jdbcsqlite3-adapter'
gem 'trinidad'
gem 'jruby-openssl'
end
gem 'jquery-rails'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug'
# Bundle the extra gems:
gem 'RedCloth', '~> 4.2.9', :require => 'redcloth'
gem 'ruby-openid', :require => 'openid'
gem 'rack-openid', :require => 'rack/openid'
gem 'aaronh-chronic', :require => 'chronic' # Fixes for 1.9.2
gem 'coderay'
gem 'lesstile'
gem 'formtastic'
gem 'will_paginate', '~> 3.0.2'
gem 'exception_notification', '~> 2.5.2'
gem 'open_id_authentication'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :test do
gem 'database_cleaner'
gem 'cucumber-rails', :require => false
gem 'cucumber-websteps', :require => false
gem 'factory_girl'
gem 'rspec'
gem 'nokogiri', '~> 1.5.0'
gem 'webrat'
end
group :development, :test do
gem 'rspec-rails'
end
And I also modified the database.yml file to look like this:
development:
adapter: mysql2
database: myblog_development
username: root
password:
host: localhost
test:
adapter: mysql2
database: myblog_test
username: root
password:
host: localhost
I am able to check the site in my local, but when trying to deploy to Heroku I get the following:
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Tasks: TOP => environment
(See full trace by running task with --trace)
Precompiling assets failed, enabling runtime asset compilation
Injecting rails31_enable_runtime_asset_compilation
Please see this article for troubleshooting help:
http://devcenter.heroku.com/articles/rails31_heroku_cedar#troubleshooting
I tried doing what they suggest in that last site, but still getting the same error. What am I missing?
In your application.rb file, add this line:
config.assets.initialize_on_precompile = false

Aptana Studio 3 - unable to remote debug rails app deployed on virtualbox

Hello I have problem to remote debugging rails app (deployed on vagrant vm) in Aptana 3.
Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'bootstrap-sass', '2.0.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'linecache19', :git => 'git://github.com/mark-moseley/linecache'
gem 'ruby-debug-base19x', '~> 0.11.30.pre4'
gem 'ruby-debug19',:require => 'ruby-debug'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.9.0'
gem 'rb-inotify', '0.8.8'
gem 'libnotify', '0.5.9'
gem "watchr", "~> 0.7"
gem 'guard-rspec', '0.5.5'
gem 'guard'
gem 'annotate', '~> 2.4.1.beta'
gem 'sextant'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.0'
group :test do
gem 'capybara', '1.1.2'
gem 'spork', '~> 0.9.0'
gem 'factory_girl_rails', '1.4.0'
end
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
rails s -u:
=> Booting WEBrick
=> Rails 3.2.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
=> Debugger enabled
[2012-06-01 06:41:48] INFO WEBrick 1.3.1
[2012-06-01 06:41:48] INFO ruby 1.9.3 (2011-10-30) [i686-linux]
[2012-06-01 06:41:48] INFO WEBrick::HTTPServer#start: pid=14225 port=3000
so debug mode is enabled
In Aptana 3:
Debug -> Debug Configurations -> Remote Ruby Debug Sessions, create new with:
host: localhost
port: 3000
Tried to run 'Debug' but I still get Problem occured dialog with 'An internal error occurred during: "Launching rails".
null'
Any idea what to do to enable aptana 3 remote debugging?
Thank you.

Installing PostgreSQL with Ruby on Rails on Mac OS X

I have everything installed. But when I run "rake db:create", I get the following:
Ken-Vogts-MacBook:sixmonths ken$ rake db:create
(in /Users/ken/sixmonths)
rake aborted!
no such file to load -- pg
Here is my database.yml:
development:
adapter: postgresql
encoding: unicode
database: sixmonths_development
pool: 5
username: postgres
password: xxxxxxxx
test:
adapter: postgresql
encoding: unicode
database: sixmonths_test
pool: 5
username: sixmonths
password: xxxxxxxx
production:
adapter: postgresql
encoding: unicode
database: sixmonths_production
pool: 5
username: sixmonths
password: xxxxxxxx
I can see pg is installed when I run: gem list
I tried replacing "postgresql" with "pg" per another post on stackoverflow, but it resulted in this:
Ken-Vogts-MacBook:sixmonths ken$ rake db:create
(in /Users/ken/sixmonths)
Seems cool, right?
Nope. Next, I try "rake db:schema:dump" and I get this:
Ken-Vogts-MacBook:sixmonths ken$ rake db:schema:dump
(in /Users/ken/sixmonths)
rake aborted!
Please install the pg adapter: `gem install activerecord-pg-adapter` (no such file to load -- active_record/connection_adapters/pg_adapter)
Of course there is no "activerecord-pg-adapter". What do I have to do to make this work?
Gemfile Contents:
source 'rubygems.org'
gem 'rails', '3.0.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'unicorn'
# gem 'capistrano'
# gem 'ruby-debug'
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
# group :development, :test do
# gem 'webrat'
# end
Rails 3 will only let you access the gems you specify in your Gemfile, so even if you have it installed in your system-wide gems by doing a gem install pg, it won't be able to find it.
Add gem 'pg' to your Gemfile, run bundle install, and you should be good to go.
I tried many things that I saw as solutions elsewhere, but the only thing that worked for me was replacing
gem 'sqlite3'
with
gem 'pg'
in Gemfile. My database.yml looks the same as yours.
Now both >rake db:create and >rake db:migrate work.
This is on OSX Snow Lepoard, Rails 3.0.7, Postgres 9.0.
And the output from:
gem list --local
is
pg (0.11.0)
polyglot (0.3.1)
postgres-pr (0.6.3)
rack (1.2.2, 1.0.1)
rack-mount (0.7.2, 0.7.1, 0.6.14)
rack-test (0.6.0, 0.5.7)
rails (3.0.7, 3.0.5, 2.3.5, 2.2.2, 1.2.6)