DEV Community 👩‍💻👨‍💻

Christian Sedlmair
Christian Sedlmair

Posted on • Updated on

Rails UJS on Vite (⚠️ together with Turbo?)

Overview

Attention ⚠️

Unobstrosive Javascript (UJS) is widely applied in many rails apps. In 5.1 the Rails team made it indepent from jQuery and moved it to the core of rails. Turbo, on the other hand, is so great!

Can UJS live alongside Turbo?
Some Conflicts are but are they so much that is in practical not possible?
Will we learn bringing them together?
See Issue on Github. My assumption is that UJS will live even for many years.

Currently i made first carefully steps coupling them. Except above issues it worked. here is how the UJS setup on vite works:

Setup

$ npm i @rails/ujs
Enter fullscreen mode Exit fullscreen mode

frontend/entrypoints/application.js

import Rails from '@rails/ujs';
Rails.start();
Enter fullscreen mode Exit fullscreen mode

Restart the server.

Test

set any link with the attribute remote: true

= link_to 'New Article', new_article_path, remote: true
Enter fullscreen mode Exit fullscreen mode

and the corresponding view

# new.js.erb
alert('hello ujs!')
Enter fullscreen mode Exit fullscreen mode

=> UJS should run

Top comments (0)

👋 Have You Posted on DEV Yet?

Head over to our Welcome Thread and tell us a bit about yourself!