|
|
|||
[Ruby on Rails] How does one render html from another web server to a Ruby on Rails app?
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? 3 Replies
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.
Comment by
Minimal Samples
: 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/" |
|||
|