How to conditionally add html.erb class in Rails

Overview

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


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

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' %>">
</div>

※Caution

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
How to install Swiper in Rails
[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 implement a like feature in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
[Rails] How to use PostgreSQL in Vagrant environment
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
[Rails] Add column to devise
How to terminate rails server
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to implement scraping
[Rails] How to make seed
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
How to deploy jQuery in your Rails app using Webpacker
How to display a graph in Ruby on Rails (LazyHighChart)
How to add the same Indexes in a nested array
Mapping to a class with a value object in How to MyBatis
Super easy in 2 steps! How to install devise! !! (rails 5 version)
[Rails] How to display information stored in the database in view
[Rails] How to write user_id (foreign key) in strong parameter
How to master programming in 3 months
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
[Rails] How to use Active Storage
How to use class methods [Java]
[Rails] How to implement star rating
How to use classes in Java?
How to name variables in Java
How to set Lombok in Eclipse
How to add the delete function
[Introduction to Rails] How to use render
[Java] How to use Math class
How to concatenate strings in java
How to delete large amounts of data in Rails and concerns
How to get boolean value with jQuery in rails simple form
I want to add devise in Rails, but I can't bundle install
How to rename a model with foreign key constraints in Rails
How to get and add data from Firebase Firestore in Ruby
How to think about class design (division) in a business system (1)
[rails6.0.0] How to save images using Active Storage in wizard format
How to implement coding conventions in Java
How to define an inner class bean
How to embed Janus Graph in Java
[Rails] [Memo] When to add = to <%%> and when not
[Rails] How to get the user information currently logged in with devise
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
How to get the date in java
[Ruby on Rails] How to use redirect_to