LoginSignup
4
4

More than 5 years have passed since last update.

railsで部分的なテンプレート・パーシャルの作り方

Posted at

railsで共通部分をまとめたい時、例えばfooterを共通にしたいときはパーシャルと言ってパーツ化する。

やり方

  • app/views/layouts/にファイルを作成する
  • ファイル名は_から始める
  • renderで呼び出す

ファイル作成

ファイルはapp/views/layouts/に作り、名前は_からはじめる。後は小文字

renderで呼び出す

application.html.erb
<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag    'application', media: 'all',
                               'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application',
                               'data-turbolinks-track': 'reload' %>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <%= yield %>
      <%= render 'layouts/footer' %>
    </div>
  </body>
</html>
4
4
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
4
4