POST 请求后外部重定向 (Ruby on Rails 7)

发布于 2025-01-13 16:35:28 字数 1447 浏览 5 评论 0 原文

在 Rails 7 中,我尝试在浏览器提交 POST 请求后使用 redirect_to 将浏览器重定向到外部网站。

我有一个带有表单的视图:

<!-- view/articles/new.html.erb -->
<h1>New Article</h1>
<%= form_with model: @article do |form| %>
  <%= form.label :mystring %>
  <%= form.text_field :mystring %>
<% end %>

控制器:

class ArticlesController < ApplicationController
  def create
    # Do some active record stuff...
    redirect_to 'https://google.com', allow_other_host: true
  end
end

和我的路线:

Rails.application.routes.draw do
  resources :articles
end

我转到 http://localhost:3000/articles/new,并将数据输入表单中。提交后,页面会重新加载(换句话说,我仍在 http://localhost:3000/articles/new 上)。正如我所期望的那样,我没有被重定向到 https://google.com

Insomnia 中执行 POST 请求,重定向工作正常,没有任何问题。

另外,我使用以下代码测试了我认为在 NodeJS 中实现此目的的方法(使用 Express)。此重定向在两种浏览器中均适用,没有任何问题。

app.post('/articles/create', function(req, res){
  res.writeHead(302, {
    'Location': 'https://google.com'
  });
  res.end();
});

根据我的理解,Rails 只是编写一个“位置”标头,与我在 Express 中手动执行的操作相同。然而,Rails 似乎确实比 Express 编写了更多的标头,因此可能其中之一就是导致问题的原因。

任何帮助将不胜感激。谢谢你!

In Rails 7, I am trying to use redirect_to to redirect the browser to an external website after that browser submits a POST request.

I have a view with a form:

<!-- view/articles/new.html.erb -->
<h1>New Article</h1>
<%= form_with model: @article do |form| %>
  <%= form.label :mystring %>
  <%= form.text_field :mystring %>
<% end %>

A controller:

class ArticlesController < ApplicationController
  def create
    # Do some active record stuff...
    redirect_to 'https://google.com', allow_other_host: true
  end
end

And my routes:

Rails.application.routes.draw do
  resources :articles
end

I go to http://localhost:3000/articles/new, and enter the data into the form. After submitting, the page simply reloads (in other words, I am still on http://localhost:3000/articles/new). I do not get redirected to https://google.com, as I expect.

Doing the POST request in Insomnia, the redirect works without any issues.

Also, I tested what I believe would be the way to accomplish this in NodeJS (using Express), with the following code. This redirect works in both browsers without any issue.

app.post('/articles/create', function(req, res){
  res.writeHead(302, {
    'Location': 'https://google.com'
  });
  res.end();
});

From my understanding, Rails is just writing a 'Location' header, same as I am doing manually in Express. Rails does seem to write quite a few more headers than Express, however, so maybe one of those is what's causing the issue.

Any help would be much appreciated. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一梦等七年七年为一梦 2025-01-20 16:35:28

遇到同样的问题,解决了。您需要将 data: { Turbo: false } 添加到您的表单方法中:

<%= form_with(model: article, method: :post, data: { turbo: false }) do |form| %>

Had the same problem, figured it out. You need to add data: { turbo: false } to your form method:

<%= form_with(model: article, method: :post, data: { turbo: false }) do |form| %>
哎呦我呸! 2025-01-20 16:35:28

对我来说,这适用于本地 localhost:3000 而不是 http://127.0.0.1:3000/ 和以这种格式,

    format.html { redirect_to(root_url(subdomain: @account.subdomain), allow_other_host: true, notice: "Account was successfully updated.") }

因为这没有

 # format.html { redirect_to root_url(subdomain: @account.subdomain), allowed_other_host: true, notification: "帐户已成功更新。" }

 格式。 html { redirect_to(root_url(subdomain: @account.subdomain), allowed_other_host: true, notification:

有关重定向的更多信息,请参见此处https: //api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to

For me, this worked on local with localhost:3000 not http://127.0.0.1:3000/ and in this format

    format.html { redirect_to(root_url(subdomain: @account.subdomain), allow_other_host: true, notice: "Account was successfully updated.") }

Where as this did not

    # format.html { redirect_to root_url(subdomain: @account.subdomain), allow_other_host: true, notice: "Account was successfully updated." }

        format.html { redirect_to(root_url(subdomain: @account.subdomain), allow_other_host: true, notice: "Account was successfully updated.") }

More info on the redirect here https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文