[Rails] Bootstrap's text-light cannot be inherited by the link_to method because the class is not written as an argument in the method.

Introduction

I introduced Bootstrap to the Rails environment, and when I thought about cleaning it up, I suddenly stumbled and shared it.

Event

It is never inherited by the element of the link_to method

White is applied to the text when written in HTML

image (2).png

index.html


<body> 
  <nav class="navbar navbar-expand-sm navbar-dark bg-dark"> 
    <ul class="navbar-nav mr-auto"> 
      <li class="nav-item"><a class="nav-link" href="">tweetApp</a></li> 
    </ul> 
  </nav>

For some reason it is not inherited when replaced with link_to

Since the navbar-dark class is set, the text color should be white, but it is blue. image (1).png

index.erb


<nav class="navbar navbar-expand-sm navbar-dark bg-dark"> 
    <ul class="navbar-nav mr-auto"> 
      <li class="nav-item"> 
        <a class="nav-link" href=""> 
          <%= link_to('tweetApp',"/") %> 
        </a> 
      </li> 
    </ul> 
  </nav>

Looking at the HTML that generated the CSS with Devtool, ...

The generated link is popping out of the a tag ...

index.erb


<nav class="navbar navbar-expand-sm navbar-dark bg-dark"> 
    <ul class="navbar-nav mr-auto"> 
      <li class="nav-item"> 
        <a class="nav-link" href=""></a> 
        <a href="/">tweetApp</a> 
      </li> 
    </ul> 
  </nav>

It seems that the link_to method has an option to specify the class

Format link_to (link text, path [, option, HTML attribute or event attribute]) class option class:" class name " Reference Rails document-About view

Write according to the format

index.erb


<nav class="navbar navbar-expand-sm navbar-dark bg-dark"> 
    <%= link_to("TweetApp","/",class: 'text-light')%> 
  </nav>

As a result, I was able to do it.

image.png

Recommended Posts

[Rails] Bootstrap's text-light cannot be inherited by the link_to method because the class is not written as an argument in the method.
An active hash that can be treated as data even if it is not in the database
[Rails] If CSS is not applied in collection_select, check if class can be specified.
[Rails 6] method :: delete cannot be used with link_to
Ebean.update () is not executed in the inherited model.
Provisional memo when the name of the method parameter of the Java class cannot be obtained by reflection in the Eclipse plug-in project.
[Ruby On Rails] In the nested state, the description in parentheses written after Prefix in the link_to method
[Rails] What to do when rails db: migrate cannot be done because there is no table referenced by the foreign key
Why the get method is needed in the Calendar class