hello. I’d like to switch with a button that clicks on the turbo_confirm message that is displayed when submitting a rails form.
The view is as below.
<%= form_with(url: entries_path, id: "entry_form", method: :post,
data: { controller: "confirmation" } ) do |f| %>
<div>
<%= button_tag type: 'submit', name: "pre_flg", value: "1",
data: { action: "click->confirmation#confirm_1", "confirmation-target": "button_1", message: t(".pre_entry_confirm") } do %>
<span class="text-xl">
pre_entry
</span>
<% end %>
</div>
<div>
<%= button_tag type: 'submit', name: "pre_flg", value: "0",
data: { action: "click->confirmation#confirm_2", "confirmation-target": "button_2", message: t(".entry_confirm") } do %>
<span class="text-xl">
entry
</span>
<% end %>
</div>
<% end %>
The stimulus controller is below.
import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = [ "button_1", "button_2" ]
confirm_1(event) {
console.log(this.button_1Target.dataset.message)
if (!(this.element.dataset.turbo_confirm = this.button_1Target.dataset.message)) {
event.preventDefault()
}
}
confirm_2(event) {
console.log(this.button_2Target.dataset.message)
if (!(this.element.dataset.turbo_confirm = this.button_2Target.dataset.message)) {
event.preventDefault()
}
}
}
Unfortunately the above stimulus controller did not display a confirm prompt.
I’d like to assign this.button_#Target.dataset.message to data-turbo_confirm element, what should I do?
If there is a different way to implement the feature, please let me know.
I’d appreciate it if you could give me some advice