Skip to content

Instantly share code, notes, and snippets.

@usutani
Created December 28, 2020 08:45
Show Gist options
  • Save usutani/54fd3931485c99858dda85c2a1dee0a1 to your computer and use it in GitHub Desktop.
Save usutani/54fd3931485c99858dda85c2a1dee0a1 to your computer and use it in GitHub Desktop.
Rails: Hotwire: button_to(delete)の見た目をlink_toに変更する(turbo-frame内の場合)
module ApplicationHelper
def form_data_window_confirm(message)
{
'data-controller' => 'window_confirm',
'data-window_confirm-message-value' => message,
'data-action' => 'turbo:submit-start->window_confirm#show',
'data-turbo-frame' => '_top'
}
end
end
<p id="notice"><%= notice %></p>
<h1>Messages</h1>
<%= turbo_frame_tag "messages" do %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @messages.each do |message| %>
<tr>
<td><%= message.title %></td>
<td><%= message.content %></td>
<td><%= link_to 'Show', message, 'data-turbo-frame': '_top' %></td>
<td><%= link_to 'Edit', edit_message_path(message), 'data-turbo-frame': '_top' %></td>
<td><%= button_to 'Destroy', message, method: :delete, form: form_data_window_confirm('Are you sure?') %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<br>
<%= link_to 'New Message', new_message_path %>
import { Controller } from "stimulus"
export default class extends Controller {
//static values = { message: String }
show(event) {
//const message = this.messageValue
const message = event.target.dataset.window_confirmMessageValue
if (window.confirm(message) == false) {
event.preventDefault()
event.detail.formSubmission.stop()
return
}
}
}
@usutani
Copy link
Author

usutani commented Dec 28, 2020

https://gist.github.com/usutani/e5151a47f637724c5527be5093d689aa
↑に、turbo-frameを追加した場合の差分。

turbo:submit-start で無理やりキャンセルしてみた。

Uncaught (in promise) DOMException: The operation was aborted.

Promiseがrejectされてしまう。処理が始まる前にキャンセルできないのだろうか。
https://github.com/hotwired/turbo/blob/aae160bed764ca7322be1e3a9e00366787d96f7f/src/http/fetch_request.ts#L86

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment