JoHksi
JoHksi

Reputation: 517

Rails bootstrap flash not fading

I am making rails app using bootstrap. Unfortunately, I cannot add whole bootstrap.CSS file to my web app because CSS are overriding each other. As an alternative solution, I just take out certain CSS from bootstrap for few things. One of them is flash. I want to show flash successfully when I send the email. Now I have two problems.

  1. Flash box shows above where nav bar is. It doesn't show up as a box on top of the screen. Look at the image below. enter image description here

What I want is below: enter image description here

  1. I put fade in CSS but it never gets triggered.

Below are my codes:

//in my MailerController.rb
flash[:success] = "Thank you for your message! We will get back to you shortly."

//application.html.erb

    <body>
        <% flash.each do |message_type, message| %>
            <%= content_tag(:div, message, class: "alert alert-#{message_type} fade in") %>
        <% end %>
</body>

CSS I took out from Bootstrap:

.alert {
  padding: 15px;
  margin-bottom: 20px;
  border: 1px solid transparent;
  border-radius: 4px;
}
.alert h4 {
  margin-top: 0;
  color: inherit;
}
.alert .alert-link {
  font-weight: bold;
}
.alert > p,
.alert > ul {
  margin-bottom: 0;
}
.alert > p + p {
  margin-top: 5px;
}
.alert-dismissable,
.alert-dismissible {
  padding-right: 35px;
}
.alert-dismissable .close,
.alert-dismissible .close {
  position: relative;
  top: -2px;
  right: -21px;
  color: inherit;
}
.alert-success {
  color: #3c763d;
  background-color: #dff0d8;
  border-color: #d6e9c6;
}
.alert-success hr {
  border-top-color: #c9e2b3;
}
.alert-success .alert-link {
  color: #2b542c;
}
.alert-info {
  color: #31708f;
  background-color: #d9edf7;
  border-color: #bce8f1;
}
.alert-info hr {
  border-top-color: #a6e1ec;
}
.alert-info .alert-link {
  color: #245269;
}
.alert-warning {
  color: #8a6d3b;
  background-color: #fcf8e3;
  border-color: #faebcc;
}
.alert-warning hr {
  border-top-color: #f7e1b5;
}
.alert-warning .alert-link {
  color: #66512c;
}
.alert-danger {
  color: #a94442;
  background-color: #f2dede;
  border-color: #ebccd1;
}
.alert-danger hr {
  border-top-color: #e4b9c0;
}
.alert-danger .alert-link {
  color: #843534;
}

.fade {
  opacity: 0;
  -webkit-transition: opacity .15s linear;
       -o-transition: opacity .15s linear;
          transition: opacity .15s linear;
}
.fade.in {
  opacity: 1;
}

Can anyone let me know what problems I'm having?

Upvotes: 0

Views: 978

Answers (1)

Seika85
Seika85

Reputation: 2021

You need to include the Bootstrap-JS for the functionality.

To position your messages like you want (e.g. out of the documents flow anywhere you want on the screen) with specific dimensions, you need a wrapper.
This Wrapper is a DIV having the dimensions you want (like width) and the position property set to absolute or fixed.

some HTML

<body>
    <div id="flash-message-wrapper">
        <% flash.each do |message_type, message| %>
            <%= content_tag(:div, message, class: "alert alert-#{message_type} fade in") %>
        <% end %>
    </div>
    <!-- more content -->
</body>

some CSS

#flash-message-wrapper {
    position: fixed;
    top: 40px; right: 100px;
    width: 500px;
}

example fiddle: https://jsfiddle.net/c4q8nn6w/1/

Using fade in initial will not render a fade effect. Just use fade and add the in per JS (e.g. on load or callback). I used an timeout to show you the actual effect.

Upvotes: 1

Related Questions