简体   繁体   中英

How can I request a Rails page from within a Rails page?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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