Jason FB
Jason FB

Reputation: 5940

New Rails 7 Turbo app doesn't show the data-turbo-confirm alert messages don't fire (turbo-rails 7.1.0 and 7.1.1)

New Rails 7 app created in 2021 or 2022, when I click on a form with data-turbo-confirm, the alert message does not show.

<%= form_with url: root_path(),  data: {'turbo-confirm': "Are you sure you want to submit this form?"},
               method: :delete do |f| %>
  <%= f.submit "Delete".html_safe, class: "delete-apple-button btn btn-primary btn-sm" %>
<% end %>


<br />
<%= turbo_frame_tag "apples-list"   do %>
  nothing has happened yet
<% end %>

The HTML produced is enter image description here

Page loads to:

enter image description here

When you click delete, no alert is shown:

enter image description here

Expected result: • Alert message confirming the button action

Actual result: • No alert is shown

Upvotes: 11

Views: 5443

Answers (3)

Candice
Candice

Reputation: 41

I ran into this same problem when doing the Ruby on Rails guide here.

Somewhere along the way of me trying things, I stumbled upon mention of the turbo-rails gem. I found a working solution with these pieces:

  1. Add this in the application.html.erb file:
    <%= javascript_include_tag "turbo", type: "module" %>
    
  2. Add gem "turbo-rails", "~> 1.0" to your Gemfile
  3. Run bundle install
  4. Run bin/rails turbo:install
    Note: not sure if this last step is necessary, but I was following instructions from here

Using these steps, the code from the rails guide works, along with the confirmation dialog. 🎉

Upvotes: 0

you can use js to do this.

the code is work for me.

function confirmDestroy(message) {
  if (!confirm(message)) {
    return false;
  }
}
<div>
  <%= link_to "Edit this post", edit_post_path(@post) %> |
  <%= link_to "Back to posts", posts_path %>

  <%= button_to "Destroy this post", @post, method: :delete, onclick: "return confirmDestroy('Are you sure want destroy this post?')" %>
</div>

enter image description here

Upvotes: 1

Jason FB
Jason FB

Reputation: 5940

This happens if you had locally installed gem versions 7.1.0 or 7.1.1 for turbo-rails

These gem numbers were pushed by accident to Rubygems in October, then yanked. However, since bundler will default to the highest number of your Rails gem when it sets up your new rails app, it will pick turbo-rails version 7.1.0 or 7.1.1 , which will display this flaw

The gems were yanked, so this only affects you if you were developing rails apps between October 2021 and the yank date.

TO FIX YOUR COMPUTER: gem uninstall turbo-rails

Bundler will prompt you for which version to uninstall: enter image description here

You will need to repeat this step if you have both gem versions installed.

Then, bundler will not make new apps with that version.

However, if you generated an app already it will be locked to the wrong version. to fix, specify version explicitly in Gemfile

gem "turbo-rails", "~> 1.0"

enter image description here

Upvotes: 1

Related Questions