1

I am using the Bootstrap modal in my app, and I am trying to execute the stock Bootstrap Modal as a proof of concept (i.e. start at the baseline that the modal works and then build up from there).

In my _notifications.html.erb I have this:

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

The issue is when I click it, the modal appears for like 1/10th of a second and then disappears.

This is what my application.js looks like:

//= require jquery
//= require jquery_ujs
//= require jquery.turbolinks
//= require best_in_place
//= require main.js
//= require bootstrap
//= require bootstrap-sprockets
//= require jquery-ui/datepicker
//= require best_in_place.jquery-ui
//= require jquery.purr
//= require best_in_place.purr
//= require bootstrap.file-input
//= require chosen.jquery
//= require spin.min
//= require ladda.min
//= require masonry.js
//= require intro.js 
//= require pnotify
//= require turbolinks


$(document).on("ready page:load", function(){
    $("input.datepicker").datepicker();
      /* Activating Best In Place && Include Success Highlighting & Bounce for comments & videos */
        $(".best_in_place").best_in_place().bind("ajax:success", function () {$(this).closest('p, h5').effect('highlight').effect("bounce", { times:3 },  { duration:400}).dequeue(); });
        $('.dropdown-toggle').dropdown();       
      $('#unread-notification').click(function(){
        var url = $(this).data('read-url');
        $.ajax({
          type: "PUT",
          url: url
        });
      });       
});

I have commented out all the JS in every other file in my app, but that still hasn't solved the issue.

What could be causing this weird behavior?

Edit 1

These are the appropriate elements in my Gemfile:

gem 'coffee-rails', '~> 4.0.1'
gem 'jquery-rails', '~> 3.1.0'
gem 'turbolinks'
gem 'bootstrap-sass', '~> 3.2.0.0'
gem 'sass-rails', '>= 3.2'
gem 'jquery-turbolinks'

To take the troubleshooting 1 step further, when I remove the class fade from the modal class, i.e. my opening div looks like this:

<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

The modal doesn't fire at all.

Edit 2

For what it's worth, when I execute/fire the modal from the JS console, I can see the modal...kinda. As in, the modal shows, but it seems to be behind the grey overlay, as you can see in the screenshot below.

modal if fired by JS console

Edit 3

Here is my bootstrap_and_overrides.css.scss declarations:

@import "bootstrap-sprockets";
@import "bootstrap";
@import "font-awesome";
marcamillion
  • 32,933
  • 55
  • 189
  • 380
  • For edit #2, this answer may help... http://stackoverflow.com/questions/20983110/bootstrap-modal-sitting-behind-backdrop/20983718#20983718 – Schmalzy Dec 05 '14 at 21:35
  • I copied your modal javascript and html and worked fine in bootply. Are you using any other javascript actions or calls to manipulate the modal? – arinh Dec 05 '14 at 21:55
  • @arinh Not as far as I know. – marcamillion Dec 05 '14 at 22:21
  • @Schmalzy That only helped for the JS console issue. Now the Modal sits on top of overlay when I execute it from the JS console. But when I try to execute the modal in the main page (via the button like the BS example) it still flashes quickly and disappears. – marcamillion Dec 05 '14 at 22:24
  • I added a live demo link so you can see it in action. – marcamillion Dec 05 '14 at 22:29
  • 1
    Try looking at this link http://stackoverflow.com/questions/12739800/turbolinks-issues-with-bootstrap-modal. If not useful I saw this could possibly be an issue for you as well http://stackoverflow.com/questions/13648979/bootstrap-modal-immediately-disappearing. Still trying to look for more. – arinh Dec 05 '14 at 22:30
  • @arinh That first link doesn't quite work for me. Largely because I am using `bootstrap-sass` and not `twitter-bootstrap` gem. – marcamillion Dec 06 '14 at 02:48
  • Possible duplicate of [Bootstrap Modal immediately disappearing](https://stackoverflow.com/questions/13648979/bootstrap-modal-immediately-disappearing) – merv Dec 29 '17 at 18:18

2 Answers2

1

After looking into, I checked out any issues you could be having. I believe your bootstrap is loaded twice as I checked through the console and each copied line presented two matches always.

Also according to bootstrap-sass docs

// "bootstrap-sprockets" must be imported before "bootstrap" and "bootstrap/variables"
arinh
  • 206
  • 1
  • 12
  • I like where you are going, but I am not quite sure where it could be being loaded twice from. I updated the question with my `bootstrap_and_overrides.css.scss` file. – marcamillion Dec 06 '14 at 02:50
  • 1
    I do think Bootstrap is being loaded twice. I also don't think I need to load `bootstrap/modal.js` as well as `bootstrap.js`, but I am not sure how to stop it. How do I force `bootstrap-sass` to just load `bootstrap.js` and not all the extensions, once? – marcamillion Dec 06 '14 at 03:04
  • I have had finals all week, sorry wasn't able to get back. However I see you resolved the solution. Glad I was able to help. – arinh Dec 10 '14 at 19:31
0

As arinh said, the issue is that Bootstrap was being loaded twice.

The main culprit was my application.js:

//= require bootstrap
//= require bootstrap-sprockets

Once I removed the top line, i.e. just converted my application.js to this:

//= require bootstrap-sprockets

It worked perfectly.

marcamillion
  • 32,933
  • 55
  • 189
  • 380