Skip to content

Instantly share code, notes, and snippets.

@johnnylambada
Last active Dec 17, 2015
Embed
What would you like to do?
rails + heroku + postgress + devise

rails + heroku + postgress + devise

This is how I actually got all this stuff to work together. It'll attrophy over time but as of June 2013 it works. Maybe I'll keep it up to date.

-- john@lombardos.org

On the shoulders...

Generally, this document is a a mashup of the following tutorials and pages...

Copy libssl & libcrypto to /usr/lib on your Mac

For me this boiled down to the following and I didn't do the symlink part.

sudo cp /Applications/Postgres.app/Contents/MacOS/lib/libcrypto.1.0.0.dylib /usr/lib
sudo cp /Applications/Postgres.app/Contents/MacOS/lib/libssl.1.0.0.dylib /usr/lib

first though, let's install some stuff we're going to need

brew install gnu-sed # this is because osx's sed is lame

DRY

Here we set up our project name, directory name and heroku project name ($PROJ.heroku.com). They don't all have to the same, but it is convenient.

export PROJ=mobgame

Create the project on your local machine

cd ~/Projects/bettracker/rails # you might choose differently
rails new $PROJ -d postgresql
cd $PROJ

Update .gitignore

cat <<EOF >>.gitignore
/public/assets/
.DS_Store
*~
/vendor/bundle
/.idea
EOF

Set up the git repo

git init
git add .
git commit -m 'rails new $PROJ -d postgresql && cd $PROJ && update .gitignore'

Create the local database

psql -c "create role $PROJ login createdb" # http://stackoverflow.com/a/8639649/9648
rake db:create

Download & install software

(cd tmp && wget http://twitter.github.io/bootstrap/assets/bootstrap.zip) && unzip tmp/bootstrap.zip -d public && rm -f tmp/bootstrap.zip
git add . && git commit -m "install bootstrap"

verify

psql -c "\list" # look for $PROJ in left col. Ctc means user can Connect Create & create Temp
psql -c "\du" # shows users

Create the Venue scaffold

rails generate scaffold Venue title:string address:string image_url:string
rake db:migrate
git add . && git commit -m "rails generate scaffold Venue"

Redirect root to /venues

gsed -i '/welcome#index/a\  root :to => redirect("/venues")' config/routes.rb
git rm public/index.html
git add . && git commit -m "Redirect root to /venues"

Add gem 'devise' to Gemfile

cat <<EOF >> Gemfile
#Use devise for auth
gem 'devise'
EOF
bundle install
git add . && git commit -m "Add 'devise' to Gemfile && bundle install"

Install devise

rails generate devise:install
git add . && git commit -m "rails g devise:install"

You'll get the following output:

Some setup you must do manually if you haven't yet:

  1. Ensure you have defined default url options in your environments files. In production, :host should be set to the actual host of your application. Here is an example of default_url_options appropriate for a development environment in config/environments/development.rb:

    config.action_mailer.default_url_options = { :host => 'localhost:3000' }
    
  2. Ensure you have defined root_url to something in your config/routes.rb. For example:

    root :to => "home#index"
    
  3. Ensure you have flash messages in app/views/layouts/application.html.erb. For example:

    <p class="notice"><%= notice %></p>
    <p class="alert"><%= alert %></p>
    
  4. If you are deploying Rails 3.1+ on Heroku, you may want to set the following on config/application.rb forcing your application to not access the DB or load models when precompiling your assets:

    config.assets.initialize_on_precompile = false
    
  5. You can copy Devise views (for customization) to your app by running:

    rails g devise:views
    

Configure devise

Define default URL in our development & production environments.

gsed -i 's/^end$/  config.action_mailer.default_url_options = { :host => "localhost:3000" }\nend/' config/environments/development.rb
gsed -i 's/^end$/  config.action_mailer.default_url_options = { :host => "'$PROJ.herokuapp.com'" }\nend/' config/environments/production.rb
git add . && git commit -m "config.action_mailer.default_url_options"

Add notice and alert flashes to app/views/layouts/application.html.erb

gsed -i '/<%= yield %>/s~.*~<% if notice %>\n  <p class="alert alert-notice"><%= notice %></p>\n<% end %>\n<% if alert %>\n  <p class="alert alert-error"><%= alert %></p>\n<% end %>\n<%= yield %>~' app/views/layouts/application.html.erb
git add . && git commit -m "notice and alert flashes"

Don't access the DB or load models when precompiling assets:

gsed -i '/^  end$/i\    config.assets.initialize_on_precompile = false' config/application.rb
git add . && git commit -m "config.assets.initialize_on_precompile = false"

Set up the devise user model

rails g devise User
rake db:migrate
git add . && git commit -m "rails g devise User && rake db:migrate"
psql ${PROJ}_development -c "\dt" # show it

The Heroku part of the Tutorial, part 1

Create the heroku project

heroku create $PROJ

Deploy to heroku

git push heroku master

Start heroku (free tier)

heroku ps:scale web=1

sync the database on heroku

heroku run rake db:create
heroku run rake db:migrate
heroku run rake db:seed

test heroku admin

open http://$PROD.herokuapp.com

Commands to reset and start over

rake db:drop
psql -c "drop user $PROJ"
heroku destroy $PROJ #--confirm $PROJ # get rid of the 1st

misc commands

heroku logs # show me the logs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment