Skip to content

Instantly share code, notes, and snippets.

@onigra
Last active August 29, 2015 13:57
Show Gist options
  • Save onigra/9829563 to your computer and use it in GitHub Desktop.
Save onigra/9829563 to your computer and use it in GitHub Desktop.
社内勉強会用rails&herokuハンズオン資料

railsコマンド叩く用のディレクトリ作成

$ gem i bundler --no-rdoc --no-ri
$ rbenv rehash
$ mkdir ror
$ cd ror
$ bundle init

# Gemfileのrailsのコメントアウト外す
$ bundle install --path vendor/bundle

railsプロジェクトの作成

$ bundle exec rails new rails_study -d mysql

gemのインストール

$ cd rails_study
$ bundle install --path vendor/bundle

サーバ起動してみる

$ bundle exec rails s
# アプリのスキーマが作成されてないのでエラーが出る

スキーマの作成

$ bundle exec rake db:create
$ bundle exec rails s
$ open http://0.0.0.0:3000/

コミット

$ echo '/vendor/bundle' >> .gitignore
$ git init
$ git add .
$ git commit

userを管理するリソースの作成

$ bundle exec rails g scaffold user name:string
$ bundle exec rake db:migrate
$ bundle exec rails s

コミット

$ git add .
$ git commit

本番環境で実行してみる

$ bundle exec rails s -e production
$ open http://0.0.0.0:3000/

エラーになる

原因1 productionのスキーマが無い
$ bundle exec rake db:create RAILS_ENV=production
原因2 productionは public/index.html が無いため、ルーティングの設定をする必要がある
$ vim config/routes.rb

# root 'users#index'
原因3 productionはjs, cssをプリコンパイルしておく必要がある
$ vim config/environments/production.rb

# config.assets.compile = true

$ bundle exec rake assets:precompile

再度本番環境で実行

$ bundle exec rails s -e production
$ open http://0.0.0.0:3000/

コミット

$ git add .
$ git commit

heroku cliのインストールとログイン

$ brew install heroku
$ heroku login

公開鍵の登録

$ heroku keys:add ~/.ssh/id_rsa.pub

ssh/configの記述

Host heroku
  User git
  HostName heroku.com
  IdentityFile ~/.ssh/id_rsa
  Port 22

アプリの作成

$ heroku create

mysql addonの設定とDBのconfig設定

$ heroku addons:add cleardb:ignite --app [app name]
$ heroku config --app [app name] | grep CLEARDB_DATABASE_URL
$ heroku config:set  DATABASE_URL='mysql2://[URL]' --app [app name]

deploy

$ git push heroku master
$ heroku run rake db:migrate
$ heroku open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment