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?

【Rails】application.html.erbについて

Last updated at Posted at 2025-04-20

記事概要

Ruby on Railsのレイアウトテンプレートについて、まとめる

前提

  • Ruby on Railsでアプリケーションを作成している

基本情報

ファイルパス

app/views/layouts/application.html.erb

記載方法

[アクション名].html.erbを、参照

まとめ(head要素)

stylesheet_link_tagメソッド

読み込むCSSファイルを指定できるヘルパーメソッド
application.cssを指定する

<!DOCTYPE html>
<html>
  <head>
    <!-- 省略 -->
    
    <!-- 読み込みたいCSSファイル(application.css)を「app/assets/stylesheets」に配置
         stylesheet_link_tagの引数に'application'を指定 -->
    <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
  </head>

  <!-- 省略 -->

まとめ(body要素)

yieldメソッド

レイアウトテンプレートに、各テンプレートファイルを展開するためのメソッド
yieldメソッドを使うことで、全ページ共通の記述を各テンプレートファイルに記載する必要がない

<!DOCTYPE html>
<html>
  <head>
    <!-- 省略 -->
  </head>
  <body>
    <header>
      <!-- 省略 -->
    </header>
    <main>
      <!-- main要素のみ、各テンプレートに記述 -->
      <%= yield %>
    </main>
    <footer>
      <!-- 省略 -->
    </footer>
  </body>
</html>

Image from Gyazo

Ruby on Railsまとめ

ビュー

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?