New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing Confirmation and Disable Controllers #29
Implementing Confirmation and Disable Controllers #29
Conversation
Realized I could solve the problem with a custom event (duh). So I pushed some changes that allow the controllers to work well together (button is no longer disabled forever when confirmation is declined). Still not 100% happy that the programmer would manually have to add Maybe this can be improved some more, but for now I'm marking this as complete. |
Hmm, yeah, I think we have to find a path, even if it requires extending Stimulus, where all you have to do is add |
I think that I have an idea. I may have to wait until this weekend to try it out though. |
static values = { message: String }; | ||
|
||
// Listen to form:submit | ||
confirm(event) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps provide a default confirmation message unless this.hasMessageValue
?
Please reopen if you come up with a solution. |
Posting this for anyone landing here from google or the hotwire discussion board. You can workaround this by adding the code below in a file and importing it. This is only for the delete confirmation. document.addEventListener("turbo:load", function () {
document.querySelectorAll("[data-confirm]").forEach(function (element) {
element.addEventListener("click", function (event) {
if (!confirm(this.getAttribute("data-confirm"))) {
event.preventDefault();
}
});
});
}); |
These features are now part of Turbo 7.1. So if you use that, you won't need to implement them yourself. data-turbo-confirm for confirms and submit buttons are automatically disabled. |
<%= button_to @course, method: :delete, data: { "confirm": "Are you sure?", "turbo-confirm": "Are you sure?"}, class: 'btn-danger' do %>
Delete
<% end %> import "@hotwired/turbo-rails";
import "@hotwired/stimulus";
import "./controllers"; @dhh Thanks for the update. I'm on freshly created Rails 7.0.0 generated with I created an example app using Rails 7.0.0 and the defaults and wasn't able to see any confirmation. You can find that repo here: https://github.com/caged/book-store. (load it up and go to /books). Let me know if I can provide more info (or if I'm mistaken). Happy to move this to a new issue if need be. |
@caged Hi there Took me a while to figure out as well. It seems that link generated this way
are now being processed as GET request and that is the reason why the turbo-confirm is being ignored. Adding an extra
resolves the issue. My guess is that the data-method attribute functionnality used to come from rails ujs |
@caged try adding the data-turbo-confirm to <%= button_to 'Delete', some_path, form:{ data:{"turbo-confirm": "Are you sure?"}}, method: "delete" %> I tried it out as a form attribute and it works fine as well . <form action="<%= some_path %>" data-turbo-confirm="Are you sure?" method="delete">
<button type="submit"> Delete </button>
</form> |
@james-em @mhulbert85 Thanks for the suggestions! 🙏🏻 I was able to get it to work. It appears that the nuance is that you need to add The docs might need an update to address this: https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to |
Leaving this in case folks arrive here from google: |
I'm making this PR in response to this discussion. So far I've managed to get these controllers working in isolation, but I'm having trouble with getting them to play well together.
I wanted to open this as a draft PR to see thoughts on how to prevent the submit event from propagating to other controllers if the confirmation is declined, regardless of invocation order. I'm considering combining the controllers into a single
form_controller
, but I'm not sure if this is appropriate. Additionally, this doesn't solve the event propagation issue for other controllers listening tosubmit
.