Rho Park
Rho Park

Reputation: 73

How to correctly use jQuery $().load(url) with Rails turbolink?

I'm very new to Rails and I am currently having a problem while using Mapbox GL JS within my rails application.

Here's what I'm trying to do. I want to click on a point on a map, and using the id from that point I'm creating a string representing the url of the page where information of that point can be viewed.

Then, by using jQuery I want to load the content of that page into a div element in application.html.erb.

However, I keep getting an error message saying, "Uncaught Error: rails-ujs has already been loaded!"

I did some searching and I see that it is an error related to use of turbolinks in Rails, but I can't figure out how out should make it work as expected.

Here is my code:

map.on('click', 'points', function(e) {

    var thing = e.features[0];
    var path = '/point/' + thing.properties.id + '.html';

        $.ajax({

            success: function() {
                $('#someDiv').load(path);
            },

            error:function(){
                alert('Error');
            }
      })
});

I apologize ahead if the way I'm doing this wrong. Thank you so much in advance. :)

Upvotes: 0

Views: 101

Answers (1)

Rho Park
Rho Park

Reputation: 73

I figured out the answer. It was because simply loading the path included everything in the head section as well, including the turbolink declaration. I just had to add div name after the path and it worked well. Thanks to anyone who tried to answer for me!

Upvotes: 1

Related Questions