Turbo ignores form ending with action ending in parameter
DaAwesomeP opened this issue · 5 comments
Hello!
I have found an issue where Turbo does not catch events for forms with an action ending in a parameter.
Example, this does not work:
<%= turbo_frame_tag "#{dom_id(chat)}_chat_message_form" do %>
<a href="/my/path">This link works and drives this specific frame!</a>
<%= form_with(model: ChatMessage.new, url: [chat, chat.chat_messages.new]) do |form| %>
<!-- form action is action="/chat/chat_messages.23" -->
<%= form.text_area :message, rows: 4, autofocus: true %>
<%= form.submit %><!-- This form submission is not caught/handled! -->
<% end %>
<% end %>
This does work:
<%= turbo_frame_tag "#{dom_id(chat)}_chat_message_form" do %>
<a href="/my/path">This link works and drives this specific frame!</a>
<%= form_with(model: ChatMessage.new, url: chat_chat_messages_path) do |form| %>
<!-- form action is action="/chat/chat_messages" -->
<%= form.text_area :message, rows: 4, autofocus: true %>
<%= form.submit %><!-- This form submission works with Turbo! -->
<% end %>
<% end %>
It's not clear to me if Turbo Rails is looking at routes.rb
and deciding not to watch the form or if Turbo doesn't like the action URL.
My routes.rb
indeed does not expect a parameter here (so I had the wrong URL), but this was incredibly difficult to debug. Rails doesn't care if a parameter is stuck on the end even if it isn't defined in routes.rb
and responds anyway. Some sort of tool (JS console?) or method or warning of why a form has been excluded from Turbo would be very nice here. This was especially confusing because the frame worked find but just not this particular form.
At one point I had added data: {turbo: true, turbo_stream: true}
and it was still ignored/not handled. There should really be a warning shown if a form is explicitly enabled but will still be ignored for some reason. I can open a separate issue for this once why it was ignored is determined.
- Rails 7.1.1
- turbo-rails 1.5.0
- @hotwired/turbo-rails 7.3.0
@DaAwesomeP thank you for opening this issue.
My hunch is that the ignored URL is related to
Lines 25 to 27 in 528dfdc
@seanpdoyle Thanks for the fast response! Yeah, that does sound like it could be it. I'm going to open a separate issue about issuing a warning if data-turbo
parameters are found in a form but the form is ignored. It seems there is some hesitation to merge #519 which would potentially solve that as well, but a warning would be nice in the meantime.
You can add a /
to the end of the path and Turbo will handle the visits correctly. /chat/chat_messages.23/
In Rails, you can use trailing_slash
option:
resources :users, trailing_slash: true
You can see more at ActionDispatch::Routing::UrlFor documentation
@DaAwesomeP Would you like to close this in favor of #608?
@brunoprietog yes, sounds good!