Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

turbolinks issues with bootstrap modal

I doing a modal like this:

Link that shows the modal:

<%= link_to "versão resumida", resumed_rep_life_animal_path(animal, :partial => true), 'data-toggle' => 'modal', 'data-target' => '#myModal', 'data-no-turbolink' => true %>

Modal html itself:

<div class="modal hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-body"></div>
  <div class="modal-footer">
    <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">Fechar</button>
  </div>
</div>

But, the data-no-turbolink dont work as expected. If I refresh the page, it works ok, but, when I browse the pages with turbolinks, looks like the data-no-turbolink is just ignored.

Am I doing something wrong? I have some modals like the example in my app, don't want to remove them and dont want to remove turbolinks neither...

Thanks in advance.

like image 715
caarlos0 Avatar asked Oct 05 '12 05:10

caarlos0


People also ask

Is Bootstrap modal responsive?

Bootstrap 5 Modal component. Responsive popup window with Bootstrap 5. Examples of with image, modal position i.e. center, z-index usage, modal fade animation, backdrop usage, modal size & more. Modal is a responsive popup used to display extra content.

How do I stop Bootstrap modal pop up?

Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.

Do you need jQuery for Bootstrap modal?

If you are using Bootstrap (js), jQuery is required.

How do I toggle Bootstrap modal?

Use the . modal(“toggle”) method in Bootstrap to toggle the modal.


2 Answers

As davydotcom said, the reason the modals aren't working is because they are bound to $(document).ready instead of $(document).on('page:change'), which is what turbolinks uses.

The jquery-turbolinks gem will make it so that ready calls will also respond to turbolink's page:change calls.

Step 1: Add gem jquery-turbolinks to your Gemfile.

Step 2: Add it to your JavaScript manifest file, in this order:

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//
// ... your other scripts here ...
//
//= require turbolinks

Step 3: Restart your server. Boom!

like image 171
Joshua Plicque Avatar answered Sep 28 '22 06:09

Joshua Plicque


data-no-turbolinks is not the issue here...

It appears bootstrap js out of the box monitors only document.ready, and bootstrap JS may need modified to check for page:load as well

Look at line 222

This will only fire on the first request in which bootstrap is included. It needs modified to fire on page:load as well.

One suggestion I can make is to use gem 'twitter-bootstrap-turbo' for getting bootstrap. This is a fork of twitter-bootstrap-rails , with the addition of turbolinks handlers.

like image 28
davydotcom Avatar answered Sep 28 '22 04:09

davydotcom