The situation came up where I needed some information from a WordPress site displayed on a Rails page. The solution had me delve into the guts of WordPress and what I did was generate a single “page” which contained only the HTML (generated from the database) that I needed.

I won’t go into detail on how to mangle with WordPress because I find it inelegant and maybe even a little ugly, but what I’ll show you now is how I rendered that information in my Rails project.The first thing we need is a URL for our rails app to scrape. For the sake of this example lets create a small list at http://myurl.com/small-list.html

Note that this does not have to be on the same server as your Rails app.  This code will be downloaded over the internet if it is available. If not, you will have to do some error handling. Next I need to write a Ruby method in my controller to download the file. I will put this in my Application Controller so it may be accessed by anything.

What the fetch_url method does is take a url as an argument and returns the value in between the and tags. If there is an error, return nil. So now it’s pretty trivial to write a small method to get our small-list.html file.

For the sake of this example lets say we want to view the small-list.html on every page contained in MyController.

Now in your view it’s as simple as adding one line to your page. This is a simple example and there are caching concerns if you are pulling information from a database but if you need an effective yet quick and dirty way to render some html from another site, here it is!

Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *