Jump to content

[Ruby on Rails] How does one render html from another web server to a Ruby on Rails app?

Minimal Samples's Photo
Posted Feb 03 2010 08:45 AM
11993 Views

Say for example one of our other depts has a php based web application that renders lists of users work schedules. I have a RoR app that allows people to search for users and see their names, phone numbers etc.

Now they want me to display the details rendered by the work shedules php app to my ruby on rails app.

Basically I need to be able to render raw html code inline on a ruby on rails page. I cant seem to get render to render the remote html inline.

EG: I search for john doe and my RoR app displays his name, phone number, email, etc and then goes to the php work schedules app and retrieves the html for john does current schedule. Then renders the returned page inline on the RoR app. (I can get the php app to just render a <div> with the details I need for the user.)

Is there an easy way to do this that I'm missing?

Tags:
2 Subscribe


3 Replies

+ 3
  Edd Dumbill's Photo
Posted Feb 04 2010 10:03 AM

It's a simple procedure to programmatically fetch a web page from another site. Here's an example of this:

  def fetch_url(url)
    r = Net::HTTP.get_response( URI.parse( url ) )
    if r.is_a? Net::HTTPSuccess
      r.body
    else
      nil
    end
  end

  # use like this from your controller
  @snippet = fetch_url "http://www.oreilly.com/"

  # and in your page <%= @snippet %>


This little function will fetch the content of URL and return it as a string. If something goes wrong, it returns a nil. Call this from your controller and stuff the result into a variable to use in your ERB template.

You'll probably want to add better error handling, but this should give you something to start with.
 : Feb 12 2010 04:13 AM
Ok so

def fetch_url(url)
r = Net::HTTP.get_response( URI.parse( url ) )
if r.is_a? Net::HTTPSuccess
r.body
else
nil
end
end

should be defined in the main controller in this case users_controller.rb



@snippet = fetch_url "http://www.myurl.com/"

Is called in the show section of the users_controller


and then <%= @snippet %> is rendered in the appropriate place in the show.html.erb file.

Is this correct? I think I must be doing someting very wrong as the app fails as soon as it executes @snippet = fetch_url "http://www.myurl.com/"
0
  chipashby's Photo
Posted Jan 07 2011 01:03 PM

Are you still looking for an answer for this?

If yes..

1) Do you want to display the schedule for all users?
OR
2) Do you want to display the schedule for each specific user on their own individual show page?