はじめに
Ruby on Rails チュートリアルの第1章を実際に行た際の手順やコマンドについて
自身の備忘録のためも含め説明いたします。
(2019.07.24追記)
手順に一部不備があったため、更新いたします。
0.Ruby on Rails とは
Webアプリの開発用のフレームワーク(Ruby言語で記述する)
1.AWS Cloud9 への登録
AWS cloud9でAWSアカウントの登録を行う。
登録後はルートユーザサインインを行い、ログインする。
※補足
- 登録時のサポートプランは「ベーシックプラン」
- Cloud9の対応ブラウザは「Chrome」or「firefox」
2.AWS Cloud9で新しいワークスペースを作成、インテンドの更新
手順に関しては図1-3(こちら)~図1-6に記載されている通り実施する。
※補足:ワークスペースを削除する場合
図1-3のところにも見える「Delete」で削除可能
3.Railsのバージョンを指定してインストール
$ gem install rails -v 5.1.6
※以降すべてAWSのbashで行う
4.アプリケーションの作成
$ rails _5.1.6_ new hello_app
コマンド実行後、「13 gems installed」と表示されれば問題ない。
※作成されるフォルダの構成については(こちら)
5.Bundlerのインストール
Gemfileを以下のように更新(リスト1.5と同様)
source 'https://rubygems.org'
gem 'rails', '5.1.6'
gem 'puma', '3.9.1'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.6.4'
group :development, :test do
gem 'sqlite3', '1.3.13'
gem 'byebug', '9.0.6', platform: :mri
end
group :development do
gem 'web-console', '3.5.1'
gem 'listen', '3.1.5'
gem 'spring', '2.0.2'
gem 'spring-watcher-listen', '2.0.1'
end
# Windows環境ではtzinfo-dataというgemを含める必要があります
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
$ cd hello_app
$ bundle install
$ bundle update
6.ローカルWebサーバーを起動する
別ターミナルを起動する
$ cd hello_app
$ rails server
※Serverの停止は「Ctrl」+「C」
7.Applicationコントローラにhelloを追加する(リスト1.7)
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def hello
render html: "hello, world!"
end
end
8.ルートルーティングを設定する(リスト1.9)
Rails.application.routes.draw do
root 'application#hello'
end
演習1-3-1
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def hello
render html: "hola, mundo!"
end
end
演習1-3-2
割愛いたします。
演習1-3-3
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def hello
render html: "hola, mundo!"
end
def goodbye
render html: "goodbye, world!"
end
end
Rails.application.routes.draw do
root 'application#goodbye'
end
9.リポジトリ上で公開するgitの設定
$ git config --global user.name "Miyasako(任意の名前)"
$ git config --global user.email 任意のメールアドレス
10.gitのセットアップ & コミット
$ cd hello_app
$ git init
$ git add -A
$ git status
$ git commit -m "Initialize repository"
※あくまでローカルマシン上での操作である
※もしも、コミットしたファイルから変更させた内容を戻したい場合
$ git checkout -f
11.Bitbucketのアカウントを作成
12.公開鍵の確認 & 作成
$ cd ~/.ssh
$ ls
このとき、「○○」「○○.pub」というファイル名の組み合わせがあるか確認する。
「○○」が秘密鍵、「○○.pub」が公開鍵になる。(例.「id_rsa」と「id_rsa.pub」)
ない場合は、公開鍵を作成する。
$ ssh-keygen
Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): id_rsaを入力
Enter passphrase (empty for no passphrase): パスフレーズを入力。
入力したくない場合は、パスフレーズを空のままにする
Enter same passphrase again: 同じパスフレーズを入力
$ ls
authorized_keys id_rsa id_rsa.pub
$ cat ~/.ssh/id_rsa.pub
ssh-rsa
AAAAB3NzaC1yc2EAAAADAQABAAABAQDK/oiK0LxiuZFnC4PGedNtJVx9d/xIPoL/o
~~~
7r18M2w2MPoQo2rLFOtv+m3c3H8kr70wgEIRNO8DNl4jnA7 ec2-user@ip-172-
31-44-34
作成した公開鍵を、catコマンドで出力し、コピー(ssh~ ~ 44-34まですべて)したのち、
Bitbucketの公開鍵設定で設定する
左下のアイコン -> Bitbucket settings -> SSH鍵
13.Bitbucketで新しいリポジトリを作成する(READMEは含めない)
※登録のユーザ名は「MiyasakoShohei」とした
14.ローカル環境にあるリポジトリをBitbucketにあげる
新しいターミナルを開く
また、bitbucketのリポジトリを確認すると、
bitvucketのサイトに接続用のコマンドが記載されている
$ cd hello_app
$ git remote add origin git@bitbucket.org:MiyasakoShohei/hello_app.git
$ git push -u origin --all
※もしも、間違えてgit remote addしてしまった場合
① まず、すでにaddした内容を取り消す。
現在登録されているディレクトリをまず確認
② その後取り消しコマンドを入力
例えば間違えてユーザ名を「Miyasako」でaddした場合
$ git remote -v
origin git@bitbucket.org:Miyasako/hello_app.git (fetch)
origin git@bitbucket.org:Miyasako/hello_app.git (push)
git remote rm origin
$ git remote -v
$ git remote add origin git@bitbucket.org:MiyasakoShohei/hello_app.git
$ git push -u origin --all
15.READMEの更新
ブランチを作成し、作成したブランチへ移動する
$ git checkout -b modify-README
$ git branch
READMEのファイルを更新し、masterへ移動、
masterにブランチの変更をマージし、ブランチを削除する
最後に、Bitbucketにpushする
$ git status
$ git commit -a -m "Improve the README file"
$ git checkout master
$ git merge modify-README
$ git branch -d modify-README
$ git push
16.Herokuのアカウントを作成
本番環境(Heroku)にデプロイする
※デプロイ・・・システムを利用可能な状態にする
17.Gemfileを修正し、gemをインストールする
$ bundle install --without production
$ git commit -a -m "Update Gemfile for Heroku"
18.Herokuのインストール & サインイン
$ source <(curl -sL https://cdn.learnenough.com/heroku_install)
$ heroku --version
heroku/7.26.2 linux-x64 node-v11.14.0
$ heroku login --interactive
herokuのアカウント登録時のメルアドとパスワード入力
19.HerokuのSSHキーを登録し、アプリケーションの実行場所を作成する
$ heroku keys:add
$ heroku create
20.デプロイする
$ git push heroku master
※Herokuにデプロイしたアプリの確認
$ heroku apps
アプリ名が表示される
※Herokuにデプロイしたアプリの情報の確認(URLなど)
$ heroku info --app <アプリ名>
21.デプロイしたアプリの名前変更
$ heroku rename <変更したいアプリ名>
あとがき
cloud9の使用が初めてなこと、またコマンドをたびたび間違えてしまい
その修復のためのコマンド確認も行ったため
完了まで約10時間くらいかかった
以上です。
ご指摘等あればよろしくお願いします。