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 3 years have passed since last update.

入力必須ではない項目の入力有無による画面の表示方法

Last updated at Posted at 2020-10-21

#環境
この記事ではmacOS Catalina10.15.6にインストールしたRuby 2.6.5を使っています。

#概要
下記の条件分岐を行うことがゴールです。
入力必須ではない項目が入力された時→詳細画面で表示(図1)
一方、入力されなかった時→詳細画面で表示されない(図2)

図1 入力された時🔽
スクリーンショット 2020-10-21 16.23.34.png

図2 入力されなかった時🔽
スクリーンショット 2020-10-21 16.25.08.png

結論 .present?を使用

if文の条件式の際に、.present?を使用することで、条件分岐することが出来ます。
そのため、@report.otherの値が存在する際には、備考の内容が表示されます。

show.html.erb
        <% if @report.other.present? %>
          <tr>
            <th class="detail-name">備考</th>
            <td class="detail-value"><%= @report.other %></td>
          </tr>
        <% end %>

以上です。
最後までお読みいただきありがとうございます!
同じことで悩んでいる方の手助けになったら幸いです!

#参考
下記のサイトを参考にして、私自身も解決出来ました。
オブジェクトの記法についてより知りたい方は、下記ご確認ください!

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?