Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

thomasvanholder
thomasvanholder

Posted on

     

How to migrate Rails UJS to Hotwire (Turbo)

SeeHotwire handbook

  1. Update hotwired/turbo-rails
  2. Remove rails/ujs
  3. Replace link_to delete
  4. Replace link_to data confirm
  5. Replace button data disable with
  6. Set status response in controller

1. Update hotwired/turbo-rails

Inpackage.json, update@hotwired/turbo-rails package to 7.1.0 (or greater)

 "@hotwired/turbo-rails": "^7.1.0"
Enter fullscreen modeExit fullscreen mode

2. Remove rails/ujs

  • From the package.json file
yarn remove @rails/ujs
Enter fullscreen modeExit fullscreen mode
  • From the application.js entry point
require("@rails/ujs").start()// remove this line
Enter fullscreen modeExit fullscreen mode

3. Replace link_to delete

method: :delete
becomes
data: {turbo_method: :delete}

<%# Old %><%link_to"Destroy",task_path(@task),method: :delete%><%# New %><%link_to"Destroy",task_path(@task),data:{turbo_method: :delete}%>
Enter fullscreen modeExit fullscreen mode

4. Replace link_to data confirm

data: {confirm: 'Are you sure?'}
becomes
data: {turbo_confirm: 'Are you sure?'}

<%# Old %><%link_to"Destroy",task_path(@task),method: :delete,data:{confirm:'Are you sure?'}%><%# New %><%link_to"Destroy",task_path(@task),data:{turbo_method: :delete,turbo_confirm:'Are you sure?'}%>
Enter fullscreen modeExit fullscreen mode

5. Replace button data disable with

In Rails UJS, a form's submit button can be disabled and its text replaced by adding the data disable_with attribute.

<%=f.button"Search",data:{disable_with:"Searching..."}%>
Enter fullscreen modeExit fullscreen mode

With Turbo, you can set the text content based on the parent's button disabled status.Source

button.show-when-disabled{display:none;}button[disabled].show-when-disabled{display:initial;}button.show-when-enabled{display:initial;}button[disabled.show-when-enabled{display:none;}
Enter fullscreen modeExit fullscreen mode
<button><spanclass="show-when-enabled">Submit</span><spanclass="show-when-disabled">Submitting...</span></button>
Enter fullscreen modeExit fullscreen mode

Or if you use Tailwind, you can leverage Tailwind'sgroup anddisabled status to create a similar effect.

<%=f.buttonclass:"group"do%><spanclass="group-disabled:hidden">Search</span><spanclass="hidden group-disabled:block group-disabled:cursor-wait">Searching...</span><%end%>
Enter fullscreen modeExit fullscreen mode

6. Set status response in controller

Respond with a303 status code

classTasksController...defdestroy@task=Task.find(params[:id])@task.destroyredirect_totasks_path,info:"Task deleted",status: :see_otherendend
Enter fullscreen modeExit fullscreen mode

Top comments(7)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
matiascarpintini profile image
Matias Carpintini

Thank you!

CollapseExpand
 
bakivernes profile image
Vernes Pendić
Hacker
  • Location
    Mostar, Bosnia and Herzegovina
  • Work
    Hacker at WizardHealth
  • Joined

Hi Thomas! I'm having issues with:link_to, url, method: :get. The link goes to a controller action that response with turbo_stream but the request arrives as HTML and I get an error after removing ujs. Tried addingdata-turbo-method="get" but because my link is inside a turbo-frame it saysResponse has no matching <turbo-frame> element. Did you by any chance encounter such issues?

CollapseExpand
 
thomasvanholder profile image
thomasvanholder
writes code: ruby on rails, stimulus, hotwire and tailwindcss.
  • Joined

@bakivernes, you can break out aturbo_frame by specifing the target attribute on a link.

link_to'root',root_path,target:"_top"# alternativelink_to'root',root_path,data:{turbo_frame:'_top'}
Enter fullscreen modeExit fullscreen mode

See alsohere

CollapseExpand
 
fcatuhe profile image
François Catuhe
  • Location
    Bordeaux, France
  • Work
    Solutions Engineer
  • Joined

Excellent, that sums up what I also discovered.

Have you investigated the replacements forRails.fire andRails.ajax? Asking as these are the 2 next on my list.

CollapseExpand
 
thomasvanholder profile image
thomasvanholder
writes code: ruby on rails, stimulus, hotwire and tailwindcss.
  • Joined

@fcatuhe, I'm glad you find it useful.

There is a recent rails library calledRequest.js which can fetch URL's in JS. This can help migrate existing Rails.fire and Rails.ajax calls.

CollapseExpand
 
fcatuhe profile image
François Catuhe
  • Location
    Bordeaux, France
  • Work
    Solutions Engineer
  • Joined

Wow I had missed that one, that's exactly it, thanks!

CollapseExpand
 
superails profile image
Yaroslav Shmarov
I write about different Ruby on Rails topics. Check it out!
  • Email
  • Location
    Chernihiv, Ukraine
  • Education
    Université Clermont Auvergne
  • Work
    Senior Ruby on Rails engineer, Bearer.com
  • Joined

very good write-up on removing ujs! thanks!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

writes code: ruby on rails, stimulus, hotwire and tailwindcss.
  • Joined

More fromthomasvanholder

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp