10

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

Lam Phan
  • 3,405
  • 2
  • 9
  • 20
Jason FB
  • 4,752
  • 3
  • 38
  • 69

3 Answers3

2

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

jscastro
  • 3,422
  • 1
  • 9
  • 27
  • Reimplementing existing features of Turbo is not a good answer. the goal is to use Turbo correctly, not to jerry-rig some other solution into my app. – Jason FB Jul 13 '23 at 14:24
1

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

Jason FB
  • 4,752
  • 3
  • 38
  • 69
0

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.

Candice
  • 11
  • 1
  • 2
  • This assumes a Node-compiled app (JSBundling) for Rails 7. If so, there's really no need to use `javascript_include_tag` for just Turbo— you can simply add `import "@hotwired/turbo-rails"` to your existing `application.js` file. – Jason FB Mar 10 '23 at 16:32
  • the biggest source of confusion is whether you are building an Importmaps app vs. JS Bundling vs Shakapacker app. This is the #1 thing people new to Rails get struck on and the specific differences between the 3 are typically where people get stuck. – Jason FB Mar 10 '23 at 16:34
  • This SO post is explicitly about the YANKED GEMS for turbo that were mistakenly pushed with wrong version numbers. That specific problem only happens for you if you had downloaded these gems between Oct-Dec 2021 and they were cached on your machine. Nobody else is affected by that specific problem. Now, getting Turbo working you might run into any number of other problems, but they are unrelated to this specific SO post. – Jason FB Mar 10 '23 at 16:35
  • All due respect, but this is NOT only due to the yanked gems. I got a new computer in Dec 2022, and this is my first time running a rails 7 tutorial (via https://guides.rubyonrails.org/getting_started.html). So it's not true to say "nobody else is affected by that specific problem". And I'm not familiar with the javascript pieces of a rails app (I'm a noob). I just used the `rails new app_name` command. I see in the help section there are different options for JS stuff, but I just left those as default since I'm just figuring things out. – Candice Mar 11 '23 at 20:55
  • with all due respect, THIS SO post, which you are on, has a title that explicitly states "(turbo-rails 7.1.0 and 7.1.1)". As well, at the top of the description, it clearly says on that THIS SO post — only this one — is related to new apps created during the affected timeframe (it says that right at the very top). That's why I say clearly "nobody else is affected." If you are not running the exact conditions described in the post -- then this is the wrong SO post for your problem, and your answer just creates noise & confusion. – Jason FB Mar 12 '23 at 17:29
  • You may have a similar problem, but SO works best when you match the exact conditions described in the original post (version numbers, installed gems, etc) to what you have. If they don't match, you're on the wrong SO post. Try to find a different SO post that more closely matches your conditions, and if you don't find one, use the "Ask Question" and make your own post for your own specific conditions & gem versions. It is better for you to use the "Ask Question", make a new post with your setup & then answer your own question right on your new post. – Jason FB Mar 12 '23 at 17:31
  • I'm recommending you use "Ask Question" to make a new post because it is better for the next person searching and better for the SO community. It has nothing to do with you. – Jason FB Mar 12 '23 at 17:36
  • also, if you used `rails new app_name` you got an importmap-rails app. I strongly suggest you familiarize yourself with the differences between working with an Importmap-rails app and a JS Bundling app. It will help you greatly as many, many people get tripped up on that part of getting started. – Jason FB Mar 12 '23 at 17:38