Rails with Turbolinks (2024)

Font Awesome integrates well with Rails and Turbolinks, especially if you use Web Fonts. But if you’re using SVG + JS, you’ll need to make a few adjustments.

We’ll cover what to do when you encounter visual glitches caused by MutationObserver, and how to install Font Awesome, whether you use a Kit or Host Font Awesome yourself, and more!

What’s the Challenge?

After you’ve installed the SVG with JS version of Font Awesome, and loaded yourfirst page in your Rails app, there are a couple of things that happen.

  1. Font Awesome will search for any <i> elements and replace them with<svg> elements.
  2. It will create a MutationObserver that watches for changes in the page todo future replacements.

When Turbolinks replaces the <body> of your page with new content afternavigation occurs, Font Awesome automatically reconnects this MutationObserverto the new content.

Here’s where the problem is: there is a flash where icons are missingbefore they quickly appear. This looks pretty nasty, but we can easily fix it.

Setting the mutateApproachconfiguration to sync (available in Version 5.8.0 or greater), provides a way to skip thisflash of missing icons by rendering them as soon as Turbolinks receives the newbody.

How To

We’re going to start by assuming that you’ve already installedTurbolinksand configured it in your Rails app.

Using Your Application Configuration

The best way is to configure it directly in the application.js file:

// This is a manifest file that'll be compiled into application.js,

// which will include all the files listed below.

//

// Any JavaScript/Coffee file within this directory, lib/assets/javascripts,

// or any plugin's vendor/assets/javascripts directory can be referenced here

// using a relative path.

//

//= require rails-ujs

//= require activestorage

//= require turbolinks

//= require_tree .

FontAwesome.config.mutateApproach = 'sync'

Using a Kit or your own external hosting

If you’ve installed Font Awesome by modifying your Rails layout, you may haveincluded a <script> tag.

Add data-mutate-approach="sync".

<!DOCTYPE html>

<html>

<head>

<title>My app</title>

<%= csrf_meta_tags %> <%= csp_meta_tag %> <%= stylesheet_link_tag

'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%=

javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

<script src="<your-kit-code>" data-mutate-approach="sync"></script>

</head>

<body>

<%= yield %>

</html>

Using the asset pipeline

Another common way to install Font Awesome is to place the source files in thevendors/assets/javascripts directory and then modify theapp/assets/javascripts/application.js to include //= require fontawesome/all. Font Awesome will then get included in the applicationbundle that your Rails layout already includes.

You can add a <script> tag in the layout to change the configuration.

<!DOCTYPE html>

<html>

<head>

<title>My app</title>

<%= csrf_meta_tags %> <%= csp_meta_tag %>

<script>

FontAwesomeConfig = {

mutateApproach: 'sync'

}

</script>

<%= stylesheet_link_tag 'application', media: 'all',

'data-turbolinks-track': 'reload' %> <%= javascript_include_tag

'application', 'data-turbolinks-track': 'reload' %>

</head>

<body>

<%= yield %>

</body>

</html>

Using Webpacker and @fortawesome/fontawesome-svg-core

You can subset icons using tree-shaking if you use Webpacker and NPM to installFont Awesome.

Your installation may look like this, in which case we simply change the configusing the API.

import { config, library, dom } from '@fortawesome/fontawesome-svg-core'

# Change the config to fix the flicker

config.mutateApproach = 'sync'

# An example icon

import {

fa*github

} from '@fortawesome/free-brands-svg-icons'

library.add(

fa*github

)

dom.watch()

Rails with Turbolinks (2024)

FAQs

How to disable turbolinks in Rails 7? ›

If you want to disable Turbolinks for certain links, add a data-turbolinks="false" attribute to the tag: < a href = "..." data-turbolinks = "false" >No turbolinks here</ a >.

How do I disable Turbolinks in Rails 6? ›

1 Answer
  1. Remove gem 'turbolinks', '~> 5' from Gemfile.
  2. Remove //= require turbolinks from app/assets/javascript/application.js.
  3. Remove , 'data-turbolinks-track': 'reload' (x2) from app/views/layouts/application.html.erb.
  4. Run yarn remove turbolinks.
  5. Run rails tmp:cache:clear.
Dec 12, 2019

What are turbo links? ›

Turbolinks is a flexible and lightweight JavaScript library aimed to make your navigation through webpages faster. Turbolinks improves webpage performance by substituting the common full-page loads for partial loads in multi-page applications.

Is Turbolinks in Rails 7? ›

When Rails 7 made its first appearance in December of 2021 a new default component was introduced called Turbo. If you have any history with Rails you might remember the Turbolinks library which is the predecessor of Turbo. Unlike JavaScript-driven applications, Rails applications are historically server-side rendered.

How does Rails Turbo work? ›

They work by streaming changes to the view via controller and updating specific elements or frames on a web page, creating a dynamic and responsive user experience. In the rails controller, we can use turbo stream to send real-time updates to the turbo frame along with a few other parameters if necessary.

What are hotwired turbo rails? ›

Hotwire is the default front-end framework for Ruby on Rails applications. It is a combination of Stimulus, Turbo, and Turbo Drive. It was introduced in Ruby on Rails 6 as an optional feature. By Ruby on Rails 7, it was included as a default option.

How do I disable Turbo on links? ›

You can disable Turbo Drive by adding data-turbo="false" directly on your links or on the parent containing them. In the above example, we created a parent div with data-turbo="false" , which disables Turbo Drive on all elements inside of it. However, we also added data: { turbo: "true" } to the second link_to .

How do I turn off Turbo in Rails form? ›

If you have this header, Rails will trigger your format. turbo_stream block and not format. html . To get around this, you could turn turbo off for that form by adding a data-turbo="false" attribute.

What is Turbo Stream in Rails 7? ›

Turbo, a modernized approach that gives Ruby on Rails a progressive enhancement, offers Turbo Streams, a tool to dynamically update multiple parts of a webpage with only a single request.

How to switch from Rails 7 to Rails 6? ›

How to upgrade to Rails 6?
  1. Step 1: Update Ruby to at least version 2.5. ...
  2. Step 2: Upgrade Rails to the latest version in 5.2 series (5.2. ...
  3. Step 3: Update the Gemfile with gem 'rails', github: 'rails/rails' and run bundle update rails. ...
  4. Step 4: Run rails app:update from terminal.
Aug 22, 2023

How do I remove gems from Rails app? ›

If you're using Rails 3+, remove the gem from the Gemfile and run bundle install . If you're using Rails 2, hopefully you've put the declaration in config/environment. rb. If so, removing it from there and running rake gems:install should do the trick.

What is Rails stimulus? ›

Stimulus for Rails. Stimulus is a JavaScript framework with modest ambitions. It doesn't seek to take over your entire front-end in fact, it's not concerned with rendering HTML at all. Instead, it's designed to augment your HTML with just enough behavior to make it shine.

What is turbo dev? ›

Turbo is an incremental bundler and build system optimized for JavaScript and TypeScript, written in Rust. The build system for JavaScript and TypeScript codebases. beta. High-performance bundler for React Server Components and TypeScript codebases.

How to use turbolinks in Laravel? ›

With Turbolinks!
  1. Install Turbolinks in your Laravel application.
  2. Add require turbolinks.js in Laravel app.js.
  3. Add data-turbolinks-track attribute to your script/CSS tags.
  4. Replace document ready with turbolinks:load in your js files.
  5. Verify all pages that app.js is loaded.
  6. (optional) Animate items if you want.
May 10, 2019

How to install turbo in Rails? ›

Installation
  1. Add the turbo-rails gem to your Gemfile: gem 'turbo-rails'
  2. Run ./bin/bundle install.
  3. Run ./bin/rails turbo:install.
  4. Run ./bin/rails turbo:install:redis to change the development Action Cable adapter from Async (the default one) to Redis. The Async adapter does not support Turbo Stream broadcasting.

How do you use Faker Rails? ›

Installation of the faker gem is very straightforward. The Faker README is very detailed and provides a great deal of information. To install the faker gem simply type gem install faker in your terminal. After the gem installation is complete, open your rails app and go to your seeds file to begin using Faker.

How to use RuboCop in Rails? ›

Including/Excluding files
  1. Here is an example that might be used for a Rails project: AllCops: Exclude: - 'db/**/*' - 'config/**/*' - 'script/**/*' - 'bin/{rails,rake}' - ! ...
  2. Here is an example: # .rubocop.yml AllCops: Exclude: - foo.rb. ...
  3. If foo.rb specified as a RuboCop's command line argument, the result is:

How to use cocoon in Rails? ›

Rails 6: Instructions on How to Implement the Cocoon Gem
  1. Make a new application for rails.
  2. The following command should be used to add jQuery to the project at this point:
  3. Update the config/webpack/environment.js file.
  4. Add the cocoon gem to the Gemfile.
  5. The cocoon gem should be added and installed using the bundle.
Feb 26, 2024

Top Articles
How much is a Universe worth on TikTok LIVE Studio?
UDP vs TCP: Why to Run Gaming Servers Separate from Chat
Cpmc Mission Bernal Campus & Orthopedic Institute Photos
Jack Doherty Lpsg
Tmf Saul's Investing Discussions
Cintas Pay Bill
Login Page
Chatiw.ib
12 Rue Gotlib 21St Arrondissem*nt
What are Dietary Reference Intakes?
Academic Integrity
Victoria Secret Comenity Easy Pay
Skylar Vox Bra Size
Shreveport Active 911
Christina Khalil Forum
065106619
Justified Official Series Trailer
London Ups Store
Abby's Caribbean Cafe
Bing Chilling Words Romanized
Indystar Obits
Hyvee Workday
Scout Shop Massapequa
Kaitlyn Katsaros Forum
Https Paperlesspay Talx Com Boydgaming
Free Personals Like Craigslist Nh
yuba-sutter apartments / housing for rent - craigslist
Two Babies One Fox Full Comic Pdf
1 Filmy4Wap In
Essence Healthcare Otc 2023 Catalog
Mami No 1 Ott
John Philip Sousa Foundation
Current Students - Pace University Online
Puffin Asmr Leak
Gus Floribama Shore Drugs
Publix Daily Soup Menu
2430 Research Parkway
Autotrader Bmw X5
Walter King Tut Johnson Sentenced
Luciipurrrr_
Exploring TrippleThePotatoes: A Popular Game - Unblocked Hub
Admissions - New York Conservatory for Dramatic Arts
The Transformation Of Vanessa Ray From Childhood To Blue Bloods - Looper
Nsav Investorshub
Swoop Amazon S3
M&T Bank
Secrets Exposed: How to Test for Mold Exposure in Your Blood!
Read Love in Orbit - Chapter 2 - Page 974 | MangaBuddy
Osrs Vorkath Combat Achievements
Mike De Beer Twitter
Intuitive Astrology with Molly McCord
Unity Webgl Extreme Race
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5548

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.