[RUBY] Use partial templates (write maintainable code)

Preface

When the portfolio was completed to some extent, I decided to rework the code. This is because the code with high maintainability is required in actual business. Specifically, by grouping the same code into one, we will improve it so that you do not have to fix multiple times when you fix it. Keep it as a memorandum so that you do not forget how to write it.

Use of partial templates

Since the header will be used repeatedly on other pages, we will summarize it as a partial template. A partial template is a mechanism to cut out code that is used repeatedly in a view file and reuse it. As a naming convention, write the underscore_ at the beginning of the file created when cutting out as a partial template. In this case, it will be _header.html.erb. Create it in the app/views/shared/_header.html.erb directory.

render method

The render method is the method used when calling the partial template. Write <% = render "Specify directory"%>. For example, index.html.erb and show.html.erb can also be reused with the render method when you want to use the header code.

index.html.erb


<%= render "shared/header" %>

<div id="home-index" class="contents row">
  <h2 class="page-title">Find your favorite!</h2>
  <p class="page-p">MY KARAOKE offers an encounter with that song that I wanted to sing.
  </p>
  <p class="page-p">This page is full of information on songs that everyone wants to sing.
Let's expand the repertoire by watching everyone's songs.
  </p>
abridgement

_header.html.erb


<header class='top-page-header wrapper'>
  <%= link_to 'MY KARAOKE', root_path, class: "title" %>

  <nav>
    <ul class="main-nav">
      <% if user_signed_in?%>
        <li><%= link_to current_user.nickname, new_user_session_path, class: "user-nickname" %></li>
        <li><%= link_to 'Log out', destroy_user_session_path, method: :delete, class: "logout" %></li>
      <% else %>
        <li><%= link_to'Login', new_user_session_path, class: "login" %></li>
        <li><%= link_to 'sign up', new_user_registration_path, class: "sign-up" %></li>
        <li><%= link_to 'Guest login', users_guest_sign_in_path, method: :post %></li>
      <% end %>
    </ul>
  </nav>
</header>

You can use the code in _header.html.erb by <% = render "shared/header"%> on the first line. By creating a partial template, you can reuse it on pages other than index in the same way.

show.html.erb


<%= render "shared/header" %>

<div class="main">
  <div class="song-show">
    <table class="detail-table">
      <tbody>
        <tr>
          <th class="show-detail-value">Contributor</th>
          <td class="show-detail-song">
            <%= link_to user_path(@song.user_id) do %>
              <%= @song.user.nickname %>
            <% end %>
          </td> 
        </tr>
abridgement

This time, I gave an example of the header for easy understanding, but of course there are other situations where partial templates can be used. For example, new and edit can be used (at least in my case) because the form etc. will be almost the same when posting a new post. It will be easier to put together from the standpoint of maintainability, so I think it is better to do it. I will.

Postscript

For the time being, I have summarized a simple partial template. I'm thinking of writing a partial template article using the locals option next time.

Recommended Posts

Use partial templates (write maintainable code)
Rails: About partial templates
How to write good code
Use native code on Android
Write Java8-like code in Java8