Railsチュートリアル第3章(2周目)埋め込みしちゃうぞ
2周目の目標は「14章までざっくり行う」です。
理解度は50%を目安にしているので、演習や細かいところはすっ飛ばします。
Railsチュートリアル第1章(2周目) はこちら。
今回の目標:第3章をざっくり行う
公式はこちら→ 第3章ほぼ静的なページの作成
約1時間で終わらせます。
必要なもの
・AWS (Cloud9)アカウント
・Bitbucket アカウント
(GitHubでも良いが、今回は忠実に進めていく)
・Heroku アカウント
行ったこと
静的なページを3つ(home、help、about)作成し、テストを行いました。
「埋め込みRuby」を使って、短いコードに修正しました。
Herokuにデプロイしました。
つまずいたこと
bundle install
またキミか・・・!
You have requested:
minitest = 5.10.3The bundle currently has minitest locked at 5.11.3.
Try runningbundle update minitest
If you are updating multiple gems in your Gemfile at once,
try passing them all tobundle update
minitestのバージョンが古いからですね。
Gemfile.lockファイルを削除、再度bundle install。
heroku create
コマンドは合っているのにエラーになりました。
git push heroku masterしても、「レポジトリが存在しない」となってしまいました。
Creating app… !
▸ You’ve reached the limit of 5 apps for unverified accounts. Delete some apps or add a credit card to verify your account.
Heroku無料版だと、appが5つまでしか作成できないからでした。
1 2 |
$ heroku apps $ heroku destroy --app |
不要なapp(1周目で作成した分)を入力し、削除します。
再度heroku createで問題ありませんでした。
実行コマンド
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
$ cd ~/environment $ rails _5.1.6_ new sample_app $ cd sample_app/ $ bundle install --without production $ git init $ git add -A $ git commit -m "Initialize repository" $ git commit -am "Improve the README" $ git remote add origin git@bitbucket.org:ユーザー名/sample_app.git $ git push -u origin --all # リポジトリやその参照先もすべてプッシュする $ git commit -am "Add hello" $ git push $ heroku create $ git push heroku master $ heroku --version $ source <(curl -sL https://cdn.learnenough.com/heroku_install) $ heroku --version $ heroku login --interactive $ heroku keys:add $ heroku create $ git push heroku master $ heroku apps $ heroku destroy --app $ heroku create $ git push heroku master $ rails generate controller StaticPages home help $ git add -A $ git commit -m "Add a Static Pages controller" $ git push origin $ cd sample_app #別ターミナルにて $ rails s $ rails test $ rails test $ touch app/views/static_pages/about.html.erb $ rails test $ git add -A $ git commit -m "Add test" $ git push $ rails test $ rails test $ git commit -am "Finish static pages" $ git push $ rails test $ git push heroku $ heroku open |
まとめ
お〜、HTMLを修正する段階に来ました。
コードを短くする「埋め込み」を使いました。
例えば、レイアウトファイルではこうなりますね。
1 |
<title><%= yield(:title) %> | Ruby on Rails Tutorial Sample App</title> |
「yield」は「産出する、引き起こす」といった意味です。
この場合だと、タイトルを自動的に引っ張ってきてくれるんですねえ。
ありがたやありがたや〜。
ここでしか読めない!Web系Masakiさんへの質問
ポートフォリオは、構想から約2ヶ月で完成したそう。
めっちゃかわいいの!
Twitterも要チェック☆
ほかにも50名以上のお話が読めるのは、このブログだけ!