Skip to content
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

Turbo ignores form ending with action ending in parameter #1069

Closed
DaAwesomeP opened this issue Nov 17, 2023 · 5 comments
Closed

Turbo ignores form ending with action ending in parameter #1069

DaAwesomeP opened this issue Nov 17, 2023 · 5 comments

Comments

@DaAwesomeP
Copy link

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
@seanpdoyle
Copy link
Contributor

@DaAwesomeP thank you for opening this issue.

My hunch is that the ignored URL is related to

turbo/src/core/url.js

Lines 25 to 27 in 528dfdc

export function isHTML(url) {
return !!getExtension(url).match(/^(?:|\.(?:htm|html|xhtml|php))$/)
}
. That means this issue might possibly be related to #608.

@DaAwesomeP
Copy link
Author

@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.

@brunoprietog
Copy link
Collaborator

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

@brunoprietog
Copy link
Collaborator

@DaAwesomeP Would you like to close this in favor of #608?

@DaAwesomeP
Copy link
Author

@brunoprietog yes, sounds good!

@DaAwesomeP DaAwesomeP closed this as not planned Won't fix, can't repro, duplicate, stale Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants