Adding remote: true Support in a Legacy Rails App

When upgrading a legacy Rails app that has buttons/links that trigger raw ajax instead of remote: true for making javascript format requests, adding remote: true can make things simpler.

After the upgrade to Rails 5+ is done, look for:

1
2
# Gemfile
gem 'jquery-rails'

If it’s not present, then add this gem and run bundle install on your terminal.

Look for require jquery and jquery-ujs files in app/assets/javascripts/application.js. If they are not present, add these lines:

If you want to use jQuery 1, you can require jquery:

1
2
3
4
// app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs

If you want to use jQuery 2, you can require jquery2 instead:

1
2
3
4
// app/assets/javascripts/application.js

//= require jquery2
//= require jquery_ujs

And if you want to use jQuery 3, you can require jquery3:

1
2
3
4
// app/assets/javascripts/application.js

//= require jquery3
//= require jquery_ujs

If you are running Rails 5.1 and up, and if you have included

1
2
3
// app/assets/javascripts/application.js

//= require rails-ujs

then jquery_ujs is not needed anymore. Instead add:

1
2
3
// app/assets/javascripts/application.js

//= require jquery

Finally, make sure that application.html.erb contains the following line as well:

1
<%= javascript_include_tag "application" %>

Now remote: true is enabled in the application!