Is it possible to disable Turbolinks for specific jQuery Ajax calls to prevent the page from refreshing and scrolling?

爱⌒轻易说出口 提交于 2019-12-04 06:41:01
Ced

Had the same problem and found a solution via another StackOverflow question.

The idea is to make a JS request to Rails (using the dataType and format options):

$.ajax({
    method: "PATCH",
    url: url,
    dataType: 'script',
    format: 'js',
    data: obj
});

In your rails controller, simply respond to this JS call with:

respond_to do |format|
    format.js
end

No page reload!

I found the answer here: Turbolinks documentation on Github.

Step 1: replace the usual document.ready with a custom turbolinks:load.

step 2: make sure to specify that the type is json

document.addEventListener('turbolinks:load', function() {
// your ajax code: 
    $.ajax({
        method: 'PATCH',
        url: url,
        dataType: 'json'
    })
}
Anthony E

Add the data-no-turbolink=true attribute to the <a> triggering the AJAX call or the <body> tag.

See: https://github.com/turbolinks/turbolinks-classic/issues/19 Another SO post with a question similar to yours: Rails 4: disable Turbolinks in a specific page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!