views:

104

answers:

3

I would like to allow users to call my ruby on rails app as a service which returns a 'div' with html content in it, and embed that div into their app (which will not be a rails application).

For example, assume someone has their own php website that has a header/footer template that gets rendered, and a content area of the page that they need to fill based on some html I generate in my rails app. I would like to allow them, from php, to call to my website, get the 'div' I generate, and embed that as html in their php page.

What I'm trying to do is host a service on my site that returns some html content, but actually show that content as part of another site, so that the end user only sees the other site and never really knows about mine.

Also, I can use javascript on the client to do this if that is the only way, but I would prefer the php app to handle this at the server if possible so the client gets the html embedded from the original server and it looks like it all was generated by the php script that generated the entire page. I also want to avoid using an iframe.

Is this possible?

A: 

Yes it's possible. The easiest way is made a partial to this embedded part.

Add a new action or a parameter on your action. When you detect it's an embedded render just your partial

render :partial => 'my_embedded'
shingara
I'm wanting to do this from a non ruby application, and have it call to my ruby app on a different web server to get the html.
NotDan
+2  A: 

Assuming you need to make a cross domain request of the data, and you can't just use an iframe, you could try using JSONP to fetch the html wrapped up in JS.

Russell Leggett
That is interesting. I think it will work, but I'd rather have the php script handle this and just return the html within the page (instead of using javascript) if possible.
NotDan
Well the php script could handle actually inserting the html, but for jsonp to work, it needs to call the callback and pass in the html. The html you return would just be in a string, so it would not really be much javascript, mostly straight up html.
Russell Leggett
+2  A: 

What you're essentially looking to do is implement a very limited RESTful API. If you are the only one who should have access, you should also implement some kind of basic security such as passing a 'secret' parameter to the Rails app to verify the requester. Simply provide a route in your Rails Application something like this (in Rails 2.3.x):

map.connect '/php/embed', :controller => 'some_controller', :action => 'embed'

Then in your controller:

class SomeController < ApplicationController
  before_filter :verify_authenticity
  def embed
    render :partial => "some_rails_partial", :layout => false
  end

  protected

  def verify_authenticity
    render(:text => 'Unauthorized', :status => 401) unless params[:secret] == 'somesecretkey'
  end
end

Then, from your PHP app, you simply need to make a call to download the contents of the URL at http://your.railsapp.com/php/embed?secret=somesecretkey.

Michael Bleigh