我有一个场景,我通过app.rb中的Typhoeus在Sinatra写了一篇文章。它看起来是这样的:
post "/send-data" do
...
request = Typhoeus::Request.new("http://localhost:4000/renders",
:method => :post,
:headers => { :Accept => "text/html" },
:followlocation => true,
:timeout => 100, # milliseconds
:params => params )
# Run the request via Hydra.
hydra = Typhoeus::Hydra.new
hydra.queue(request)
hydra.run
...
end当我发布到'send-data‘Typhoeus成功完成它的发布,并将用户推送到创建的记录的视图(http://localhost:4000/renders/34634646464),这是一个rails应用程序。
问题是用户永远不会从/send-data被重定向,所以如果您刷新页面,它会再次尝试执行post。我想这是有道理的,但我真的需要将用户重定向到记录的最终(url)位置。换句话说,可以看到新的记录,但这种重定向方法实际上并不会将用户从sinatra应用程序中移出。
处理这个问题的最好方法是什么?我脑海中唯一能想到的就是不使用'followlocation',而是让/send-data控制器操作在从Typhoeus获得响应位置后执行重定向。
发布于 2012-12-29 13:13:53
我试过我的建议,它起作用了。而且看起来也不算太糟。
request = Typhoeus::Request.new("http://localhost:4000/renders.json",
:method => :post,
:headers => { :Accept => "json" },
:timeout => 100, # milliseconds
:params => params )
hydra = Typhoeus::Hydra.new
hydra.queue(request)
hydra.run
response = request.response
redirect response.headers_hash['Location']我确实必须在我的rails服务器上进行更改。rails创建操作以json作为响应,'Location‘是它的返回值。‘location’是新创建的记录所在的位置,然后我只需执行Sinatra重定向,它将重定向到rails应用程序上的新记录。
https://stackoverflow.com/questions/14078718
复制相似问题