In looking at the API, I was surprised to see that using a URL to
render a partial on some remote server wasn't a possibility. I can
think of situations where that would be useful. Is there some
alternative means of accomplishing that objective? Thanks for any
input.
This is called XSS, or Cross Site Scripting. It’s a serious security hole and as such is prohibited by Javascript. You can only make Ajax calls to the same host.
This is called XSS, or Cross Site Scripting. It's a serious security hole
and as such is prohibited by Javascript. You can only make Ajax calls to the
While I realize that partials are used primarily in conjunction with
Ajax, they can be used elsewhere. I'm not talking about Ajax. I am
simply looking for the best way to include a chunk of HTML code (i.e.,
a piece of a full HTML document) that is served from one web server
within the HTML pages served by other web servers. It's like a remote
include.
I hate to keep hammering on this; but, I am hopeful that if I persist
I'll eventually be able to get across what I'm looking for. An iframe
won't meet my needs for 2 reasons: (1) It places the content in a
separate frame which can then be scrolled or whatever. I don't want a
separate frame. I want to insert (include if you will) a remote
chunk of html code into the current document. (2) iframes require a
complete html document. I want to include a chunk of html code which
is not a complete html document into the current html document. In
short, I would like it to work just like a partial except that the
source code for the partial is obtained from another server cross the
network.
Well, the only thing I can think of doing is building an HTTP request in your controller action and dealing with the resulting HTML as you wish. Assuming that by “source code of the partial” you mean it’s still in Erb form, then you’ll have to download the file from that remote server, save it in a retrievable place, which then makes it available as a regular partial. Of course you can also use wget and treat the response as a string as well, rendering it or running it through Erb as needed.
Absolutely. Let's say that you're a manufacturer of widgets for sale
to the general public. You have a number of affiliates who market
your widgets through their respective web sites. Without using
frames, you want to provide master content on your web server that can
be incorporated into the respective websites of your affiliates.
Since you control the content you don't have to be concerned that the
content being touted by your affiliates is out of date. Similarly,
each your affiliates doesn't have to update their web sites whenever
there is some change affecting the widget content such as a price
change. You just update your master and it magically propogates to
all of your affiliates. Finally, and this is one of the reasons that
I don't want to use frames, if one carefully crafts both the content
being served by your web server and the respective pages of the
affiliates into which that content is inserted, an external style
sheet can be used to style the content. That means that each of your
affiliates can style the content that you provide so that it
integrates nicely into their particular web site.
So, unless we can come up with something better, maybe I should look
at your wget suggestion. What do you think?
There's this thing called a webservice (whether it's REST, XML-RPC, SOAP, …) that is meant to do exactly that: provide a link to remote data. The only reason why you'd want to use the remote wgetting or curl'ing, or whatever, is if you have no control over the application on the other side. This probably isn't the case, as you want the remote server to serve you partials.
There are some really nifty features in EdgeRails (or available as a plugin) that are aimed at providing an easy and DRY method for accessing remote data through REST and it's called ActiveResource. It's like ActiveRecord for REST services. Ryan has been writing up some blog posts on this, they might help you get a better idea of the possibilities: ryandaigle.com
Upon reflection, I decided this whole thing seems to be way too
complicated
for what I'm trying to accomplish. I decided to revisit Jason's
suggestion of
using wget. The only problem with it is it's a bit clumsy and lacks a
bit in
the elegance department. Then I reasoned that implementing a wget
method
in ruby probably isn't all that difficult. Upon further reflection I
guessed that
someone had probably already done it. So, I turned to Google to see
what
I could find. It turns out that this is incredibly easy to
implement. So here's
the source for my helper method:
def remote_include_partial(url)
require 'open-uri'
out=""
open(url){ |f| out=out+f.read }
end
I am a neophyte in Ruby programming. So, if anyone has any
suggestions
for improvement, I'd love to hear them.
Including a require within a method seems a bit strange to me; but, I
don't
know how else to do it within the context of a helper.
I'm thinking about expanding the code so that it will work with both
remote
and local partials. The code would test to see if the url begins with
'http:'.
If it does, it's remote else it's local. If I do that, I'm wondering
if there might
be a better name for the argument now named 'url'.
Sometimes, it's inconvenient dealing with partials. It might be nice
to have
them as complete html documents. So, my second contemplated
improvement
would be to optionally allow for removing any beginning content down
to and
including any opening <body> and also removing any closing <body> and
anything that follows.