0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails チュートリアル 第3章 まとめ

Last updated at Posted at 2019-10-18

commit -am はaddを省略することができるが、commitした内容がメッセージと違うと開発に影響が出るので慎重に使うか、使わないこと

git branchの説明

git checkout -b sikousakugo

25:00 StaticPagesコントローラーの作成

git `static_pages/home` 

この書き方で書くことはほとんどない

テスト
ブラウザを毎回確認しなくても、rails testを行うことでチェックできる

test "should get home" do
    get static_pages_home_url #getリクエストをURLに送る
    assert_response :success  #必ず何らかのレスポンスが返ってくるはず
    assert_select"title":"Home | Ruby on Rails Tutorial Sample App" 
# titleタグの中身は右のようにあるべきというチェック

  end

テスト駆動開発という開発方法
まずあるべき姿をテストで書き、エラーメッセージを見ながら修正することを繰り返す

リファクタリング
app/views/layouts/application.html.erbの<%=yield%>にそれぞれのビューが入る(それぞれのビューがその度に呼ばれる)
applicationテンプレートとhomeテンプレートは入れ子構造になっている
applicationテンプレートのtitleタグ以外は共通なので、erb(埋め込みRuby)を使えば冗長さをなくしたコードが書ける

applicationを使わずに、headの部分なども全てhome.htmlで記述した場合、以下のようになる

<!DOCTYPE> 
html>
<html>
  <head>
    <title>Home | Ruby on Rails Tutorial Sample App</title>
  </head>
  <body>

erbの機能として

home.html.erb
<% provide(:title, "Home") %> #titleという変数にHomeという文字列を代入
<% yield(:title) %> #provideで宣言した変数を利用できる

がある。
この二つの機能を使うと

home.html.erb
<% provide(:title, "Home") %>
<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | Ruby on Rails Tutorial Sample App</title>
  </head>
  <body>
   

このように書き換えることができる。

yieldを使った共通化はしっくりこない部分もあるが、yieldのたびにhomeなどのページが呼ばれているとまずは理解しよう。この章での一番の難関はリファクタリングの理解

root "----"は初期表示ページへのルーティング
git push heroku master のmasterはbranch名

10/22 二周目

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?