The process of using Rails Composer to create a rails app using postgreSQL on Cloud9 and deploying on Heroku is fraught with warnings and errors that can confound a new developer. Are there any instructions that can help me get through the process and deploy my starter app?
I believe in the rails composer, cloud9 and heroku and the latter's recommended postgreSQL database though using them all together has a lot of little details that no one source clarifies how to get around. After spending many nights working it out, I've created the following list of instructions which works as of the publish date and time.
Set up a postgreSQL database on your workspace:
$ sudo service postgresql start
$ sudo sudo -u postgres psql
postgres=# CREATE USER username SUPERUSER PASSWORD 'password';
CREATE ROLE
postgres=# \q
Choose a name of the app that is available on heroku since it is either one that you have or one still available on heroku. You can check availability by visiting [potential domain name].herokuapp.com
$ mkdir ["your app's directory/domain name"]
$ cd ["your app's directory/domain name"]
The current gems used by Rails Composer use ruby version 2.2.3 so that must be installed in rvm with the following code:
$ rvm install 2.2.3
$ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
$ rvm use ruby-2.2.3@[your app name] --ruby-version --create
$ gem install rails --no-ri --no-rdoc
Create some local variables and save some values to them.
$ echo "export USERNAME=username" >> ~/.profile
$ echo "export PASSWORD=password" >> ~/.profile
Enter your app's domain name as a local environmental variable with the following code on the cl:
$ echo "export DOMAIN_NAME=[domain name].herokuapp.com" >> ~/.profile
Next, devise requires that a super long secret key be set as an environmental variable. Once a rails app is created, rake can generate such a secret key using the $ rake secret
command on the cl. Since we've not yet used composer to generate our rails app, rake won't work. We'll do the same thing, though, using irb and securerandom as described by James Badger in his blog post, Generate a New Secret Token for Rails Apps:
$ irb
2.2.3 :001 > require 'securerandom'
=> true
2.2.3 :002 > SecureRandom.hex(64)
=> "137d8b4bf436e670e2eea63372494b84aa25900edb1328eb5c1367f5100fe114fc95313f8772428dbda89ed84086e87a26428ef524951f94fd0375d4e399b613"
2.2.3 :003 > exit
We (you and me) should use a different one in production.
$ echo "export SECRET_KEY_BASE=137d8b4bf436e670e2eea63372494b84aa25900edb1328eb5c1367f5100fe114fc95313f8772428dbda89ed84086e87a26428ef524951f94fd0375d4e399b613" >> ~/.profile
RESTART TERMINAL so that ENV variables are set. Right-clicking in terminal and selecting ‘restart all terminals’ will do it in cloud9. Restarting your terminals may return you to the root directory. If that's the case, change to your new app directory at the cl using:
$ cd [your app directory]
Now you're all set to rev up Rails Composer
$ rails new . -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb
What follows are my rails composer choices which may be useful to you though yours may vary.
build a starter app?: 3 -- elected to create custom rails app
development server: 4 - puma
production server: 1 - same as development
development database: 2 - PostgresSQL
template engine: 2 - Haml
test enviroment: 2 - rspec with capybara
continuous testing: 1 - none
frontend framework: 2 -- bootstrap 3.3
email support: 1 -- none
authentication: 2 -- devise
devise modules: 1 -- devise with default modules
authorization: 1 -- none
form builder gem: 2 -- simpleform
Add pages: 5 -- Home, About, and Users
Add Bootstrap page templates? [You can check out the options at the startbootstrap.com templates webpage.]
set a local?: [return/nothing for English]
page-view analytics: 2 -- Google Analytics
generated a google analytics ID on google and entered it in.
prepare for deployment: 2 -- heroku
Disable Rails Turbolinks?: n
ban spiders: n
create github repository: n -- I plan on using bitbucket
add gem and file for environmental variables?: 1
reduce assets logger noise during development: y
improve error reporting with ‘better_errors’ during development? y
use ‘pry’ as console replacement during development and test: y
use ‘rubocop’ to ensure that your code conforms to the Ruby Style guide? y
create a project-specific rvm gemset? y
Add ‘therubyracer’? n
[Creating app!]
Username for PostgreSQL (leave blank to use the app name) username # given what I entered above to create the local pg database
Host for PostgreSQL in database.yml? (leave blank to use default socket connection) left blank
password # also given what I entered above to create the local pg database
Okay to drop all existing databases named [your app name]? y
commit final changes on git There's one final commit that Rails Composer seems to skip for some reason... Do the following:
$ git add .
$ git commit -am “Final rails_composer commit”
follow directions on bitbucket to create new repository for existing project and push to it from command line
follow instructions for ‘getting started with rails’ on heroku which includes the following:
$ heroku login
Enter your heroku credentials at the prompts.
$ heroku create [your app name (without the herokuapp.com stored as a local variable above)]
Verify heroku remote was added with the following command:
$ git config --list | grep heroku
$ git push heroku master
Now that you've created the app on heroku, you've got to add some environmental variables there, too, with the following commands:
$ heroku config:set DOMAIN_NAME=[your app name].herokuapp.com
$ rake secret # uses rake to generate a new secret key
5ed8c7d9a3bda9cec3887b61f22aa95bf430a3a550407642b96751c7ef0ce8946a161506d6739da0dcaaea8c8f4f8b3335b1fb549e3cc54f0a4cec554ede05f8
Cut and paste that new secret key into the command below to set a Heroku environmental variable.
$ heroku config:set SECRET_KEY_BASE=5ed8c7d9a3bda9cec3887b61f22aa95bf430a3a550407642b96751c7ef0ce8946a161506d6739da0dcaaea8c8f4f8b3335b1fb549e3cc54f0a4cec554ede05f8
Note: You should use $ rake secret
to generate a different SECRET_KEY_BASE than the one I've included in the code above.
Now you can migrate the heroku databases:
$ heroku run rake db:migrate
Create a Procfile
$ touch Procfile
[Procfile]
web: bundle exec puma -C config/puma.rb
$ touch config/puma.rb
[config/puma.rb]
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Worker specific setup for Rails 4.1+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
ActiveRecord::Base.establish_connection
end
Because I don't expect my app to get too much traffic and can't guarantee that it's thread-safe:
$ heroku config:set MIN_THREADS=1 MAX_THREADS=1
Commit that Procfile and puma configuration file with:
$ git add .
$ git commit -m "Add Procfile and config/puma.rb"
$ git push && git push heroku
I hope that these instructions are useful. Please feel free to add to them and improve them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With