如何从 Rails 页面中请求 Rails 页面?
某个操作必须根据 uri
参数加载 html 页面。它适用于所有有效的 uri,除了当前服务器的 uri 之外。有没有一种简单的方法可以从控制器内请求页面?
require 'uri'
class MainController < ApplicationController
def foo
uri = URI(params[:uri])
if uri.host =~ /localhost|mydomain\.com/
# what goes here?
else
@html = Net::HTTP.get(uri)
end
# Do something useful with @html...
end
end
# The code should work for http://localhost/foo?uri=http://apple.com/
# as well as for http://localhost/foo?uri=http://localhost/bar
A certain action must load an html page according a uri
parameter. It works fine for all valid uris, except those of the current server. Is there an easy way to request a page from within a controller?
require 'uri'
class MainController < ApplicationController
def foo
uri = URI(params[:uri])
if uri.host =~ /localhost|mydomain\.com/
# what goes here?
else
@html = Net::HTTP.get(uri)
end
# Do something useful with @html...
end
end
# The code should work for http://localhost/foo?uri=http://apple.com/
# as well as for http://localhost/foo?uri=http://localhost/bar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要进行区分呢?如果没有有关您要完成的任务的更多详细信息,我只会为任何 URL 调用
Net::HTTP.get(uri)
。显然,您应该过滤掉/foo
以避免无限循环。Why make a distinction? Without more details regarding what you're trying to accomplish, I would just call
Net::HTTP.get(uri)
for any URL. You should obviously filter out/foo
to avoid an infinite loop though.