Fix the submitter’s formmethod not being respected when using Rails 7 #button form helper #445
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
After upgrading turbo-rails from 7.1.0 to 7.3.0, code like this broke:
Before the upgrade, turbo correctly issued a request with method
POST
, and the body containing a_method
key with thedelete
value:This made Rails correctly route it with a
delete
verb on the backend.After the upgrade, however, the
_method
key in the body is lost:I could trace back this to https://github.com/hotwired/turbo-rails/pull/370/files from @seanpdoyle.
That code carefully looks for the submitter's
formmethod
attribute. However, there's an extra piece in this puzzle: Rails 7ActionView::Helpers::FormBuilder#button
has an override forformmethod
if the submitter does not have thename
orvalue
attributes set, which is the default.This means that if you use <%= f.button formmethod: :delete %>, it will generate a
<button name="_method" value="delete" formmethod="post">
.So, on Rails 7, looking at the submitters
formmethod
is not enough, because the HTML in the page will already have swapped theformmethod
defined by the user topost
, setting the desiredformmethod
in thevalue
field instead.Therefore, this PR fixes the bug by checking if the submitter's name is already
_method
, in which case the submitter'svalue
attribute will contain the actually desired method.Lastly, I need help finishing the test suite. I couldn't understand exactly how to make so that the tests use the file
app/javascript/turbo/fetch_requests.js
file, because it seems to use theapp/assets/javascripts/turbo.js
file instead? And that file seems to be turbo + turbo-rails combined? Couldn't figure this one out!@seanpdoyle Maybe you could help me finish this? Thanks in advance!
Mentioning hotwired/turbo#564 because it's related.