##References
##Use the latest ruby and setup gemset
rvm list known rvm install ... rvm use ... rvm gemset create myapp gem install bundler gem install rails
##Create Rails App
Command to create new rails app:
rails new myapp --database=postgresql
Command to go to app directory:
cd myapp
##Rails Setup
Add a controller:
rails generate controller root
In file app/views/root/index.html.erb write:
Hello world!
In file config/routes.rb, on line 2 add:
root 'root#index'
At the end of Gemfile add:
gem 'rails_12factor', group: :production
In file config/database.yml remove all instances of:
username: myapp
At the end of Gemfile add:
ruby '2.1.2'
Make sure that postgres is installed with a default user that can create a database.
Run the following to create the database:
bundle exec rake db:create
Run the server:
rails server
Setup unicorn:
Add the following to the Gemfile
gem 'unicorn'
Add the following file to
config/unicorn.rb
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3) timeout 15 preload_app true before_fork do |server, worker| Signal.trap 'TERM' do puts 'Unicorn master intercepting TERM and sending myself QUIT instead' Process.kill 'QUIT', Process.pid end defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! end after_fork do |server, worker| Signal.trap 'TERM' do puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT' end defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end
Finally create a Procfile with:
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
##Rev Up Git
git init git remote add origin [github url] git add . git commit -m "Initial commit" git push origin master
##Rev Up Heroku Setup the correct account:
heroku accounts:set myaccount
###Create dev and prod environments command to create dev:
heroku create --remote dev heroku rename myapp-dev --remote dev
command to create prod:
heroku create --remote prod heroku rename myapp-prod --remote prod
###Install Heroku add-ons (prod example) command to add pg backup app:
heroku addons:add pgbackups:auto-week --remote prod
command to run migrations:
heroku run rake db:migrate --remote prod
###Set up NewRelic
Command line instruction
heroku addons:add newrelic:standard
Add this line to the Gemfile
gem 'newrelic_rpm'
Run the install
bundle install
Add the script generated from Heroku newrelic addon to a new file named config/newrelic.yml
##Set up Gemfile
add to gemfile:
group :development, :test do gem 'sqlite3' end group :production do gem 'pg' end
Be sure to remote gem 'pg'.
command:
bundle install
##Set up RSpec
add to gemfile:
group :test do gem 'rspec-rails' end group :development do gem 'rspec-rails' end
command:
bundle install
command:
rails generate rspec:install
##Set Up ActiveAdmin
add to gemfile:
gem 'activeadmin', github: 'gregbell/active_admin' gem 'devise'
command:
bundle install
command:
bundle exec rails generate active_admin:install
command:
bundle exec rake db:migrate
test your setup and sign-in at:
http://[site.com]/admin un: admin@example.com pw: password
After pushing, remember to run rake on Heroku
heroku run rake db:migrate
go to config/environments/development.rb and add this line:
config.action_mailer.default_url_options = { :host => 'localhost/3000' }
go to config/environments/production.rb and add this line:
config.action_mailer.default_url_options = { :host => 'domain.of.site.com' }
create config/initializers/mail.rb and add this script:
ActionMailer::Base.smtp_settings = { :address => 'smtp.sendgrid.net', :port => '587', :authentication => :plain, :user_name => ENV['SENDGRID_USERNAME'], :password => ENV['SENDGRID_PASSWORD'], :domain => 'heroku.com' } ActionMailer::Base.delivery_method = :smtp
###Register/Setup the AdminUser with ActiveAdmin
command:
rails generate active_admin:resource AdminUser
go to app/admin/admin_users.rb and add this block:
ActiveAdmin.register AdminUser do index do column :email column :current_sign_in_at column :last_sign_in_at column :sign_in_count default_actions end form do |f| f.inputs "Admin Details" do f.input :email end f.buttons end end
go to app/models/admin_models.rb and add this line:
after_create { |admin| admin.send_reset_password_instructions }
add this block:
def password_required? new_record? ? false : super end
###Set Up Some Models
command:
rails generate model Post name:string
command:
rake db:migrate
###Register a model with ActiveAdmin
command:
rails generate active_admin:resource [ModelNameSingular]
###Edit ActiveAdmin config
-
go to config/initializers/active_admin.rb for basic settings
-
go to app/admin/dashboards.rb for the dashboard
###Adjust for the mass assignment security changes
Make this edit to your respective model files at /app/models/model_name.rb:
class User < ActiveRecord::Base
attr_accessible :firstname, :lastname, :age
end
##Handle Error Pages
Add to config/routes.rb file:
match '*a', :to => 'errors#routing'
Create app/controllers/errors_controller.rb and add this script:
class ErrorsController < ApplicationController def routing render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false end end
###Create a boring view at app/views/static/monitor.html.erb
Monitoring Page
###Create a boring template at app/views/layouts/monitor.html.erb <title>IndyCar Laps</title> <%= stylesheet_link_tag "application", :media => "all" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %> <%= yield %>
###Set up your controller file at app/controllers/static_controller.rb # GET /monitor def monitor render :layout => 'monitor' end
###Set up the route in your config/routes.rb file match 'monitor' => 'static#monitor'
##Push to Prod or Dev
command to add files to git:
git add .
command to commit files to git:
git commit -m "installing ActiveAdmin"
command to push to Github:
git push origin master
command to push to Dev:
git push dev master
command to push to Prod:
git push prod master
##Errors
###Locked out of the default admin user
Run the console:
heroku run console OR ruby console
Run this line in the rails or heroku console:
AdminUser.create!(:email => 'admin@example.com', :password => 'password', :password_confirmation => 'password')