LoginSignup
5
4

More than 3 years have passed since last update.

Railsでhtml.erbのclassを条件付きで追加する方法

Last updated at Posted at 2020-07-08

概容

下記のクラスに「ある条件」の時だけクラスを追加したいとする。


<div class="container">
</div>

例えば今回はusers_controllerの時だけクラスを追加するとする。
その場合の条件式は下記のようになる。


<div class="container<%= ' user-container' if controller_name = 'users' %>">
</div>

※注意

追加したいクラス名の先頭文字の前はスペースを開けること。


正しい: <%= ' user-container' if・・・
間違い: <%= 'user-container' if・・・

このような条件付きでclassを追加することによってその条件下のみでCSSでスタイルの変更や上書きができるようになるのでぜひ覚えておきたい。

5
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
5
4