This post describes how to setup an environment to develop a Ruby on Rails application and deploy it on Heroku.com.
Heroku is a Platform-as-a-Service (PaaS) that provides deployment environment on the cloud.
Ruby on Rails web application is well-supported by Heroku in addition to other environment, such as Java.
Utilizing Heroku and Git significantly simplifies the development and deployment process, otherwise very difficult tasks.
This tutorial extensibly uses Rails and Git, and focuses the development and deployment process.
See Setting Up Ruby on Rails on Windows and Git Basic Guide (on Windows) for more detail.
Below describes setting up the environment for the development and deployment.
GitHub is used as the central repository for the revision control.
Two Heroku applications/repositories are created for the staging and production deployment.
Staging is used as an intermediate testing environment, to avoid any unexpected interference for the production.
git config --global alias.commit-all '!git add -A && git commit'
rails new new-app-name
rails new new-app-name --skip-test-unit
cd new-app-name
Especially, enable 'sqlite3' gem only for development (heroku uses PostgreSQL).
Also, add 'rspec-rails' and 'capybara' for testing.
group :development, :test do
gem 'sqlite3'
gem 'rspec-rails'
end
group :production do
gem 'pg'
end
group :test do
gem 'capybara'
end
gem 'bootstrap-sass'
(May have to specify version for each gem to match with the deployment environment)
bundle install --without production
bundle install
rails generate rspec:install
git init
git commit-all -m "Create new application"
git remote add origin [email protected]:username/repository-name.git
git push -u origin master
git push
heroku create --stack cedar --remote staging
heroke rename app-name-staging --remote staging
git push staging master
git push staging
heroku open --remote staging
heroku create --stack cedar --remote production
heroke rename app-name-production --remote production
git push production master
git push production
heroku open --remote production
heroku domains:add www.example.com --remote production
git mv README.rdoc README.md
rails generate controller ControllerNames action_name1 action_name2 --no-test-framework
rails destroy controller ControllerNames action_name1 action_name2
Add a 'get' line in config/routs.rb:
AppName::Application.routes.draw do
…
root to: 'controller_names#action_name'
resources :controller_names
get 'controller_names/action_name'
match '/some_name', to: 'controller_names#action_name'
…
end
Add a function in app/controller/controller_names.rb:
class ControllerNamesController < ApplicationController
…
def action_name
end
…
end
rails generate model ModelName field1:data-type field2:data-type
rails destroy model ModelName
rails generate migration add_new_column_name_to_model_name new_column_name:data-type
bundle exec rake db:migrate
bundle exec rake db:rollback
bundle exec rake db:migrate VERSION=0
bundle exec rake db:reset
bundle exec rake db:test:prepare
rails generate integration_test controller_names
bundle exec rspec spec/requests/controller_names_spec.rb
rails console
rails c
exit
rails console --sandbox
rails server
rails s
git commit-all -m "commit message"
git push
git push staging
heroku run rake db:migrate --remote staging
heroku open --remote staging
git push production
heroku run rake db:migrate --remote production
heroku open --remote production