简体   繁体   English

如何从Rails页面中请求Rails页面?

[英]How can I request a Rails page from within a Rails page?

A certain action must load an html page according a uri parameter. 某个操作必须根据uri参数加载html页面。 It works fine for all valid uris, except those of the current server. 它适用于所有有效的uri,当前服务器除外。 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

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. 如果没有更多关于您要完成的工作的详细信息,我将只为任何URL调用Net::HTTP.get(uri) You should obviously filter out /foo to avoid an infinite loop though. 您显然应该过滤掉/foo来避免无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM