rails `form_with` `redirect_to` 303 `:see_other` makes the request does not redirect

Viewed 338

I have a Rails form on a "show" view that looks something like:

<%= form_with(url: "/relative-path-to-index-view", method: "post") do |f| %>
  <%= f.submit "Submit" %>
<% end %>

The create action of the controller responsible for handling the form submit looks like this:

def create
  redirect_to action: "index", status: :see_other
end

When I submit the form, I see in the network tab that it made the POST request, received a 303 :see_other response, and then made a GET request to the index action. However, the end-user doesn't see any redirect in the browser. Any ideas on why?

Firefox network tab

1 Answers

āœ… This was fixed by simply appending local: true to the form_with parameters.

- <%= form_with(url: "/relative-path-to-index-view", method: "post") do |f| %>
+ <%= form_with(url: "/relative-path-to-index-view", method: "post", local: true) do |f| %>

https://guides.rubyonrails.org/form_helpers.html

Rails Docs Guidance for local: true

Related