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

Fix the submitter’s formmethod not being respected when using Rails 7 #button form helper #445

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/javascript/turbo/fetch_requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ function determineFetchMethod(submitter, body, form) {

function determineFormMethod(submitter) {
if (submitter instanceof HTMLButtonElement || submitter instanceof HTMLInputElement) {
if (submitter.hasAttribute("formmethod")) {
// Rails 7 ActionView::Helpers::FormBuilder#button method has an override
// for formmethod if the button does not have name or value 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">. Therefore, if the submitter's name
// is already _method, it's value attribute already contains the desired
// method.
if (submitter.name === '_method') {
return submitter.value
} else if (submitter.hasAttribute("formmethod")) {
return submitter.formMethod
} else {
return null
Expand Down
5 changes: 5 additions & 0 deletions test/dummy/app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def destroy
redirect_to articles_url
end

def destroy_all
Article.destroy_all
redirect_to articles_url, status: :see_other
end

private

def article_params
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

<button>Submit as PATCH</button>
<button formmethod="post" formaction="<%= articles_path %>">Submit as POST</button>

<%# In Rails 7, there's an override of formaction, so `form.button formaction: destroy_all_articles_path, formmethod: :delete` would generate the HTML below. In Rails 6, the override of `formaction` does not exist yet, so we generate the HTML manually. %>
<button name="_method" type="submit" formaction="<%= destroy_all_articles_path %>" formmethod="post" value="delete">Delete all articles</button>

<% end %>

<%= link_to "Submit as DELETE", article_path(@article.id), data: { turbo_method: :delete } %>
4 changes: 3 additions & 1 deletion test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Rails.application.routes.draw do
resources :articles
resources :articles do
delete :destroy_all, on: :collection
end
resources :messages do
collection do
get :section
Expand Down