为什么我提交时我的浏览器不会转到新页面?
在我看来,我有一个按钮,可以将数据发布到我的控制器中并在其中执行操作。
我的咖啡脚本:
$.post "/token/create", json
TokenController#创建
redirect_to root_path, notice: 'Parser was successfully created.'
日志文件看起来很有效:
Started POST "/token/create" for 127.0.0.1 at 2014-10-08 10:27:39 -0400
Processing by TokenController#create as */*
...
Redirected to http://localhost:3000/
Completed 302 Found in 37ms (ActiveRecord: 10.0ms)
Started GET "/" for 127.0.0.1 at 2014-10-08 10:27:40 -0400
Processing by VisitorsController#index as */*
...
Completed 200 OK in 2891ms (Views: 2886.6ms | ActiveRecord: 3.0ms)
但我所在的页面并没有改变。如何使页面转到我在“redirect_to”行中指定的任何页面?
答案 0 :(得分:3)
您可以执行以下操作:
def create
# do some stuff
if request.xhr? # test if the request is an AJAX call
render js: "document.location = '#{root_path}'"
else
redirect_to root_path, notice: 'Parser was successfully created.'
end
end
您还希望在AJAX案例中使用flash
吗?检查这个答案:https://stackoverflow.com/a/18443966/976775(并且很好,如果对你有帮助就把他的答案投票给他人)
答案 1 :(得分:0)
因为您正在执行ajax调用。 您必须通过javascript更改document.location作为响应
create.js.erb
document.location = <%= root_url.inspect %>
例如