LoginSignup
18
15

More than 3 years have passed since last update.

【Rails】turbolinksを無効化する方法

Posted at

開発環境

・Ruby: 2.5.7
・Rails: 5.2.4
・Vagrant: 2.2.7
・VirtualBox: 6.1
・OS: macOS Catalina

完全に無効化する方法

1.Gemを無効化

Gemfile
# コメントアウトする
# gem 'turbolinks', '~> 5'
ターミナル
$ bundle update

2.application.jsを編集

=を削除する。

application.js
// 変更前
//= require turbolinks 

// 変更後
// require turbolinks 

3. application.html.slimを編集

'data-turbolinks-track': 'reload'を削除する。

application.html.slim
/ 変更前
= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'

/ 変更後
= stylesheet_link_tag    'application', media: 'all'
= javascript_include_tag 'application'

部分的に無効化する方法

1.JavaScriptを編集する方法

~.jsファイルの場合

~.js
$(document).on('turbolinks:load', function() {
  // turbolinksを無効化したい処理
});

~.coffeeファイルの場合

~.coffee
$(document).on 'turbolinks:load', -> 
  # turbolinksを無効化したい処理

2.リンクを編集する方法

①link_toに属性を追加する場合

~html.slim
= link_to '', root_path, 'data-turbolinks': false

②divで囲う場合

~html.slim
div data-turbolinks='false'
  = link_to '', root_path
18
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
15