【Ruby on Rails】環境設定からHello World!まで
・環境設定
AWS Cloud9を使う。
・AWSアカウントを作成する。すでに持っている場合はスキップでOK。
・環境を新規作成する
AWS Cloud9 Amazon Web Services
・プロジェクト環境名を任意名称で入力する。
・Railsをインストールする。
$ printf "install: --no-rdoc --no-ri\nupdate: --no-rdoc --no-ri\n" >> ~/.gemrc
$ gem install rails -v 5.1.6
・ディレクトリ作成
$ cd # 进入家目录 $ mkdir environment # 新建 environment 目录 $ cd environment/ # 进入 environment 目录
・アプリ作成
$ rails _5.1.6_ new hello_app
・Bundler
$ cd hello_app/
$ bundle install
・Server起動
$ cd ~/environment/hello_app/
$ rails server
・プレビュー
ここまで環境設定が完了する。
▼app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def hello
render html: "hello, world!"
end
end
▼config/routes.rb
Rails.application.routes.draw do
root 'application#hello'
end
保存してもう一回プレビューすると、Hello World!が表示される。
補足)Herokuを使ってデプロイする。
・Heroku登録
クラウド・アプリケーション・プラットフォーム | Heroku
・Heroku CLIをインストール
source <(curl -sL https://cdn.learnenough.com/heroku_install)
・インストール確認
heroku --version
・ログイン
heroku login
・herokuでアプリを作成する
heroku create
・push
git push heroku master
ここまでherokuへのデプロイが完了する。
・リネーム
heroku rename newName
⇒http://newName.herokuapp.com/