-
Notifications
You must be signed in to change notification settings - Fork 329
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
"data-turbo-method: delete" should use POST with _method rather than DELETE #259
Comments
This could be a Turbo configuration option that is automatically turned on when using turbo-rails ? |
I'm thinking something similar to what was done for confirm_method. So Turbo doesn't need to know about the Rails specific POST/_method trick. |
For reference, the |
What about a including a controller concern that overrides This is what I'm using in an application I'm upgrading: # frozen_string_literal: true
module Turbo
module SafeRedirection
extend ActiveSupport::Concern
def redirect_to(url = {}, options = {})
result = super
if !request.get? && options[:turbo] != false && request.accepts.include?(Mime[:turbo_stream])
self.status = 303
end
result
end
end
end The above is adapted from the Would it be reasonable to add that to |
Thanks for all things Rails. I'm excited to learn more about Hotwire etc. I am observing the same behavior described in this issue, but with PATCH methods, so I thought I would add what I can. I had this older Rails 5 app with the following routes:
In my view, I generated "publish" and "unpublish" links like so:
Worked fine in Rails 5, the anchor tags incorporated the I have custom actions in the controller:
They do the usual "if update redirect here otherwise redirect there" idiom. Nothing fancy. After upgrading to Rails 7, the links stopped working. I learned that we should now use I am now witnessing weird behavior. The "unpublish" link half-works and the "publish" link does not. In the case of "publish," the log shows the request is sent to #update. In the case of the "publish" link, the logs shows one PATCH request for #publish:
Followed by twenty (!) more PATCH requests to #update. They look like this:
Yes, there are twenty of them. In addition, the view does not update. However, when I click the "unpublish" link, the view does update. That said, I see one PATCH request to #unpublish:
Followed by one PATCH request to #update:
Followed by a GET request which was generated by the logic in #publish
Was I misinformed about replacing Why does one link not work at all and generates 20 additional requests, while the other link kinda works and does not generate the 20 requests but still generates the extra PATCH #update request? |
@hfpp2012 You don't need to do that. You just replace @ybakos What are the redirects like on those actions? Turbo will follow redirects and use the same HTTP method where Turbolinks would follow the redirect with HTTP GET. You may need to adjust the redirect to use |
@dwightwatson They all redirect to the same view the link appears on. The source is below. But wait! Do I need to use
|
You do not need See this comment:
|
Why can't the redirect status be changed to 303 as default when using turbo-rails? This is not intuitive at all for newbies. |
I think #370 solves this. Anyway, why can't make redirect_to redirect with 303 status instead of 302? Thanks |
I agree having an option to change redirect_to to use 303 - See Other by default would be great. I see also Rails docs mentions this issue: https://api.rubyonrails.org/v7.0.3.1/classes/ActionController/Redirecting.html
|
When an
<a>
link attributed with 'data-turbo-method: delete' is clicked turbo intercepts the click and issues a DELETE request to the server.It's likely that the Rails controller will delete a record and then issue a redirect_to to a different page. The default status code for redirect_to is 302. This causes an issue with turbo, as it then issues the subsequent request while retaining the http method (i.e. DELETE). This can be avoided by explicitly issuing the redirect as a 303 e.g.
redirect_to root_path, status: 303
An example sequence of requests illustrating the issue is:
That
DELETE "/"
gives me the heebie-jeebies 😲.We can't change the default redirect status in Rails, but don't want to have to remember to set the status code each time.
Proposed solution is to leave (base) turbo as is, and modify turbo-rails to use the POST w/_method approach. Other backends will need to respond with a 303. The POST w/_method approach is Rails specific.
See discussion here: rails/rails#43430
I think the method convertLinkWithMethodClickToFormSubmission needs to be extended to handle this case. I'm really not sure how that will be done to avoid issues when base turbo is subsequently upgraded. Watching with interest!
The text was updated successfully, but these errors were encountered: