【问题标题】:rails `form_with` `redirect_to` 303 `:see_other` makes the request does not redirectrails `form_with` `redirect_to` 303 `:see_other` 使请求不重定向
【发布时间】:2026-01-27 22:05:02
【问题描述】:

我在“显示”视图上有一个 Rails 表单,看起来像:

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

负责处理表单提交的控制器的create 操作如下所示:

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

当我提交表单时,我在网络选项卡中看到它发出了 POST 请求,收到了 303 :see_other 响应,然后对 index 操作发出了 GET 请求。但是,最终用户在浏览器中看不到任何重定向。关于为什么的任何想法?

【问题讨论】:

    标签: ruby-on-rails forms redirect


    【解决方案1】:

    ✅ 只需将 local: true 附加到 form_with 参数即可解决此问题。

    - <%= 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

    【讨论】: