在 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!
发布评论
评论(2)
遇到同样的问题,解决了。您需要将
data: { Turbo: false }
添加到您的表单方法中:Had the same problem, figured it out. You need to add
data: { turbo: false }
to your form method:对我来说,这适用于本地 localhost:3000 而不是 http://127.0.0.1:3000/ 和以这种格式,
因为这没有
有关重定向的更多信息,请参见此处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
Where as this did not
More info on the redirect here https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to