LoginSignup
9
10

More than 5 years have passed since last update.

Rails (4.2.6)+Github+Heroku開発環境を手っ取り早く用意する (enPiT 2016 PBL用)

Last updated at Posted at 2016-07-15

インストール

  • Windows 10で試してみました
    • 追記: Mac OS X El Capitanでもやってみましたが同じ手順で全く問題ないです。
  • 前提として Git for windowsがインストールされていて、Git bashが使えることを想定します。
  1. Virtualboxをインストールする
  2. Vagrant をインストールする
  3. Git bash(Macの人はターミナル)を起動させる
  4. 作業用ディレクトリに移動し、enPiT16スタータキットをダウンロードする

     % cd work
     % git clone https://github.com/enpitut2016/enpit16-rails426 
     % cd enpit16-rails426
    
  5. vagrant upすると仮想マシンがダウンロードされ、設定・起動する

     % vagrant up
    

(追記) boxファイルのダウンロード前にエラーになった場合

  • 特にエラーメッセージが出るはずのところ、何も表示されない時
  • 以下のURLに書いてあることを試してみてください。

Rails環境の確認

  1. vagrant sshで仮想マシンにリモートログインする

     % vagrant ssh
    
  2. ゲストOS上で以下のコマンドを実行してみてRails環境が動くかを確認する

     % git clone https://github.com/yasslab/sample_apps.git
     % cd sample_apps/4_2_2/ch11
     % bundle install
     % bundle exec rake db:migrate
     % bundle exec rake db:test:prepare
    
     # テストが通ることを確認する
     % bundle exec rake test
    
     # rails server が立ち上がるか確認する
     % bundle exec rails server -b 192.168.33.10 -p 3000
    
     # ホストOS (お手持ちのパソコン) のブラウザを立ち上げ、
     # アドレズバーに http://192.168.33.10:3000/ を打ち込んで画面が開くか確認する
    

現時点の疑問

  • 本当はIPアドレスを指定しなくても http://localhost:3000 をホストOS側のブラウザで指定すれば見れるはずなんだけど、できませんでした。なぜだろう…。

Githubの設定をする

  • 設定用のスクリプトをインストールしているので実行する

    % github-connect.sh
    Enter your github username : (ユーザ名を入れる)
    Enter your github password : (パスワードを入れる)
    Set git committer email {xxx@xxx.xx} : (エンターキーを押す)
    Set git committer name {xxx xxx} : (エンターキーを押す)
    Enter passphase : (公開鍵用のパスフレーズを入れる)
    Enter same passphrase again: (もう一度入れる)
    
  • このスクリプトを実行することで以下の設定が完了します

    • コミット時に必要なユーザ名とメールアドレスの設定
    • push時に必要な認証のための公開鍵設定

最小限のRailsアプリを作って動かしてみる(hello worldレベル)

  • Rails tutorialの第1章を参考にやります。
  • hello_appを作って動かしてみる

     % cd ~/workspace
     % rails new hello_app
     % cd hello_app
     % vi Gemfile (→ リスト1.5をコピーペーストして保存)
     % bundle install
     % vi app/controllers/application_controller.rb
       (→ リスト1.8を参考にhelloを定義する)
     % vi config/routes.rb
       (→ リスト1.10を参考にapplication#helloへのルートを追加)
     % rails server -b 192.168.33.10 -p 3000
     (ホストOS側で http://192.168.33.10:3000 を開いてhello, world!がでたらOK)
    

Githubのレポジトリを作ってアップしてみる

  1. Gitレポジトリをローカルに作ってコミットする

    % cd ~/workspace/hello_app
    % git init
    % git add -A
    % git commit -m "Initialized repository"
    
  2. Githubレポジトリを作ってpushする

    • http://github.com/new でレポジトリを作る
    • repository nameはhello_appにする
    • 以下のコマンドを実行してpushする

      % git remote add origin git@github.com:(アカウント名)/hello_app.git
      % git push -u origin master
      

Herokuへデプロイする

  1. 本番環境用の設定をGemfileに追加する

     % vi Gemfile
     (リスト1.14を参考に:production用の設定を追加する)
     % bundle install --without production
     % git commit -a -m "Update Gemfile.lock for Heroku"
    
  2. Herokuのレポジトリを作る

    • 事前にherokuのアカウントを作っておきましょう。
     % heroku create
     Enter your Heroku credentials.
     Email: (メールアドレスを入れる)
     Password : (パスワードを入れる)
    
  3. Herokuにデプロイする

     % git push heroku master
    
  4. pushが終わった後で表示されたURLを開いてみる

9
10
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10