How to Remove Turbolinks from Rails 6
Turbolinks began shipping in Rails 4 and while it has some advantages, it also comes with some complications that sometimes you just don't want to deal with. For this reason many developers choose to remove it, especially for simple projects where its benefits are negligible.
Starting a New Project
If you are starting a new project, the simplest thing to do is pass the flag
--skip-turbolinks which will generate your project without Turbolinks turned on. If this is the case for you, you're all done.
rails new project-name --skip-turbolinks
Removing from an Existing Project
If you need to remove Turbolinks from a project which has already been generated, you can simply follow the following steps. For our purposes we will create a new project but without skipping the Turbolinks installation.
rails new tutorial-remove-turbolinks cd tutorial-remove-turbolinks/
Next we need to create a controller and a view for our new project.
rails generate controller hello world
/config/routes.rb file replace it with the following to set our new controller and action as our root path.
Rails.application.routes.draw do root 'hello#world' end
Inside of the
/app/views/hello/world.html.erb file that was created, replace it with the following:
Here we are checking for the Turbolinks
load event handler and if it executes then it will update the
<h2> status element. This will tell us whether we have successfully removed it or not.
Remove or comment out the Turbolinks line in your
# gem 'turbolinks', '~> 5'
Gemfile.lock as well.
application.js, remove or comment out the line that requires Turbolinks.
application.html.erb layout file, remove the Turbolinks keys.
Refresh your browser and you should see "No" in the answer to our question. The reason for this is that Turbolinks is no longer loaded so the event never fires and the value in that headline never changes.