How to display external site in rails view?

I am trying to create a view in my Rails application that displays a page on an external site. So basically, instead of linking to an external page, this external page will be shown in my view already.

Is there a way to do this without using an iFrame, or is this the only way?

+3


source to share


2 answers


You can use Javascript to load content from an external site and display it in your view.

Here is the code:



<script>
    $("#externalSiteContent").load("http://www.example.com/index.html");
</script>
<div id="externalSiteContent"></div>

      

+4


source


This is a universal approach:

class YahooController < ApplicationController

  layout false

  def show 
    url = URI.parse('http://www.yahoo.com/')
    req = Net::HTTP::Get.new(url.to_s)
    res = Net::HTTP.start(url.host, url.port) {|http|
      http.request(req)
    }

    @body = res.body
  end

end

      

In view



# views/yahoo/show.html.haml
= @body.html_safe

      

You will need a route to this, of course.

# routes.rb
get '/yahoo', :to => 'yahoo#show' 

      

+2


source







All Articles