Zach S Zach S - 1 month ago 8
Ajax Question

Rails Ajax Error: Turbolinks undefined

I'm using Rails 4.2 and I want to send an ajax request to my server. But the request always fails with the error

Turbolinks is not defined
. My rails console returns a 200 ok response.
I am using the 'jquery-turbolinks' gem.

Thanks in advance!

Ajax

$.ajax({
type: "PUT",
url: "http://localhost:3000",
data: xx,
success: function(data){
alert("success");
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("some error");
}
});


Controller

def upvote
# code here
@vote.save
respond_to do |format|
format.html { redirect_to :back }
format.json { head :ok }
format.js { head :ok }
end
end

Answer

In application.js, are you requiring jquery.turbolinks between jquery and turbolinks like below?

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require masonry/jquery.masonry
//= require turbolinks
//= require_tree .

If your turbolinks version is 5, jquery-turbolinks gem doesn't support turbolinks 5. So, at the moment, you have to add JS for compatibility reffering this issue (see Lowryderch's comment). Put the turbolinks-compatibility.coffee under app/assets/javascripts/ folder.

Comments