Hitsuji_monのブログ~ 村上春樹のあれ ~

文学好きな組み込み系エンジニア

Rails 日記App制作 ~メッセージ表示: notice~

記事を投稿したり、更新したら「投稿しました!」「更新しました!」みたいなメッセージ欲しいよね
ということで、flashメソッドのオプションnoticeを使う。
あとレイアウト崩れ問題が解決

レイアウト崩れている時のコード

    <h1>投稿一覧</h1>
    <div class="ml-auto posts_button">
        <!-- 修正-->
        <%= link_to '投稿', new_post_path, class: "btn btn-outline-info" %> 
    </div>
</div>
<table class="table table-bordered border-primary table-hover table-sm posts_table">
  <thead class="bg-info text-light">
    <tr>
      <th scope="col">No.</th>
      <th scope="col">投稿タイトル</th>
      <th scope="col">投稿者</th>
      <th scope="col">投稿日</th>
      <th scope="col">更新日時</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
      <% @posts.each do |post| %>
        <tr>
            <th><%= post.id %></th>
            <td><%= post.title %></td>
            <td><%= post.name %></td>
            <td><%= post.created_at.to_s(:datetime_base) %></td>
            <td><%= post.updated_at.to_s(:datetime_base) %></td>
            <td><%= link_to "詳細" , post, class: 'btn btn-outline-info' %></td>
            <td><%= link_to "削除" , post, class: 'btn btn-outline-info', method: :delete, data: {confirm: "投稿記事を削除しますか?", cancel: "キャンセル", commit: "削除する", title: "削除の確認"} %></td>
        </tr>
      <% end %>
  </tbody>
</table>

<!-- 追加 -->
<%= paginate @posts %>

レイアウトが直ったコード

<div class="d-flex align-items-center"> <!-- これがいつの間にか消えてた center配置のクラス宣言 -->
    <h1>投稿一覧</h1>
    <div class="ml-auto posts_button">
        <!-- 修正-->
        <%= link_to '投稿', new_post_path, class: "btn btn-outline-info" %> 
    </div>
</div>

<!-- 追加 -->
<% if flash[:notice] %>
    <div class="alert alert-primary"><%= flash[:notice] %></div>
<% end %>

<table class="table table-bordered border-primary table-hover table-sm posts_table">
  <thead class="bg-info text-light">
    <tr>
      <th scope="col">No.</th>
      <th scope="col">投稿タイトル</th>
      <th scope="col">投稿者</th>
      <th scope="col">投稿日</th>
      <th scope="col">更新日時</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
      <% @posts.each do |post| %>
        <tr>
            <th><%= post.id %></th>
            <td><%= post.title %></td>
            <td><%= post.name %></td>
            <td><%= post.created_at.to_s(:datetime_base) %></td>
            <td><%= post.updated_at.to_s(:datetime_base) %></td>
            <td><%= link_to "詳細" , post, class: 'btn btn-outline-info' %></td>
            <td><%= link_to "削除" , post, class: 'btn btn-outline-info', method: :delete, data: {confirm: "投稿記事を削除しますか?", cancel: "キャンセル", commit: "削除する", title: "削除の確認"} %></td>
        </tr>
      <% end %>
  </tbody>
</table>

見返し用

あと寝てる間にMacbookAirの画面上にものが落下したらしく、「液晶割れ」
クッソ、修理代たけえな。今はモニタ出力してるから開発できるてるけど。