How to conditionally add html.erb class in Rails


Suppose you want to add a class to the following class only when there are "certain conditions".

<div class="container">

For example, suppose this time we add a class only when ʻusers_controller`. The conditional expression in that case is as follows.

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


Leave a space before the first letter of the class name you want to add.

correct:<%= ' user-container'if ...
mistake:<%= 'user-container'if ...

By adding class with such a condition, you can change or overwrite the style with CSS only under that condition, so please remember.

Recommended Posts

How to conditionally add html.erb class in Rails
[Rails] How to add new pages
How to introduce jQuery in Rails 6
How to install Swiper in Rails
How to change app name in rails
How to use custom helpers in rails
How to insert a video in Rails
How to add jar file in ScalaIDE
How to use MySQL in Rails tutorial
[rails] How to configure routing in resources
How to implement ranking functionality in Rails
How to use credentials.yml.enc introduced in Rails 5.2
How to get keycloak credentials in interceptor class
How to write Rails
How to add / remove Ruby on Rails columns
[Rails] How to use select boxes in Ransack
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
How to add sound in the app (swift)
How to get Class from Element in Java
How to add a classpath in Spring Boot
How to separate .scss by controller in Rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
How to set the display time to Japan time in Rails
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
[Ruby On Rails] How to reset DB in Heroku
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
How to query Array in jsonb with Rails + postgres
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
[Rails] How to display an image in the view
How to add ActionText function
[Rails] How to use enum
How to read rails routes
How to use java class
How to use rails join
How to number (number) with html.erb
[Rails] Add column to devise
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
How to add HDD to Ubuntu
[Rails] How to use Scope