You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
= form_with model: @parent do |form|
= form.fields_for :users, user do |user_form|
= turbo_frame_tag dom_id(user) do
= user_form.label :first_name
= user_form.text_field :first_name
= link_to 'Delete', users_path(user), method: :delete <!-- (1) -->
<!-- OR -->
= button_to "Delete", users_path(user), method: :delete <!-- (2) -->
& the following model
class Parent < ApplicationRecord
has_many :users
accepts_nested_attributes_for :users
end
& the following controller
class UsersController < ApplicationController
def destroy
respond_to do |format|
format.turbo_stream do
user = User.find(params[:id]).destroy
# Must match the dom_id in the view
render turbo_stream: turbo_stream.remove("users_#{user.id}")
end
end
When attempting to delete the nested user frame through the following:
link_to (1):
This works with Rails UJs as the data-method="delete" is POSTed to the users#destroy
This is troublesome as we are using a <a> tag to perform a destructive action. As mentioned, this is a no-no for a11y compliance.
button_to (2):
Translated to an <input> tag, as there is already a parent form tag present
On submission: this sends a submit request to parents#destroy action
Is a11y compliant, but is incompatible with turbo.
Is there a way to do this natively in turbo, or will have to sprinkle in some Stimulus to wire this up correctly.
Or should I not be trying to use a nested form with a turbo tag in such a way?
The text was updated successfully, but these errors were encountered:
You can't have nested forms, that's right. So that's a no go. It's an interesting conflict. If you're set on this particular UI, I'd probably hook up a button to a stimulus controller. Agree it's not graceful though.
@allengreer-latero how did you end up solving this? I was trying @dhh s suggestion, but I don't know how I can trigger a post request from my stimulus controller that will result in my turbo frame being refreshed. I'm relatively new to rails development, any pointers are massively appreciated!
Related discussion I found: #33, #50
If I have the following in a partial
view
:& the following
model
& the following
controller
When attempting to delete the nested user frame through the following:
link_to
(1):<a>
tag to perform a destructive action. As mentioned, this is a no-no for a11y compliance.button_to
(2):<input>
tag, as there is already a parentform
tag presentIs there a way to do this natively in turbo, or will have to sprinkle in some Stimulus to wire this up correctly.
Or should I not be trying to use a nested form with a turbo tag in such a way?
The text was updated successfully, but these errors were encountered: