user3317171
user3317171

Reputation: 41

How do I display an external site in my rails view?

I'm trying to build a view in my Rails app that mirrors a page on an external site. So basically, instead of linking to the external page, that external page will be shown in my view already.

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

Upvotes: 3

Views: 2073

Answers (2)

Mark Swardstrom
Mark Swardstrom

Reputation: 18080

This is an all-rails 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 the view

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

Of course, you'll need a route to this.

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

Upvotes: 3

Arslan Ali
Arslan Ali

Reputation: 17802

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

Here is the code:

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

Upvotes: 4

Related Questions