What is the Ruby on Rails equivalent to php includes?

If I was to include a file (such as in a template) in php to use
across a large number of pages, I would write this:

<?php include ('navigation.html'); ?>

In turn, every time I used this code, it would “include” (or better
put, display) the content of navigation.html in the browser.

I can’t figure out how to do this is Rails. When building my layouts,
how can I include a file like this, like I would in PHP. I have files
I’d like to include remotely, but not sure how to get this done.

Any ideas?

On Wed, Sep 30, 2009 at 10:44 AM, Eric J. Gruber [email protected]
wrote:

I’d like to include remotely, but not sure how to get this done.

Any ideas?

render :partial => ‘foo/bar’


Greg D.
http://destiney.com/

I tried that, with something like this:

<%= render :partial => ‘http://example.com/navigation.html’ %>

And I got the “We’re sorry, but something went wrong.” page from my
app.

I am 100% positive the server I’m trying to include the file from
allows files to re used remotely.

On Wed, Sep 30, 2009 at 11:00 AM, Eric G. [email protected]
wrote:

<%= render :partial => ‘http://example.com/navigation.html’ %>

The partial file should be local to the Rails app.


Greg D.
http://destiney.com/

You can’t do that. render works only with files local to your app.

Jason

OK … so is there another way to do it without using render?

Surely this isn’t something that PHP can do that Rails can’t.

Right. That helps if the file I am using is locally. But what do I do
if that file is from another server?

On Sep 30, 2009, at 9:06 AM, Eric G. wrote:

OK … so is there another way to do it without using render?

Surely this isn’t something that PHP can do that Rails can’t.

This is a terrible idea. But hey, here, have a loaded gun:

<%= open(“http://example.com/navigation.html”).read %>

~ j.

If there’s some sort of risk, please inform.

Not that it matters, that didn’t work either.

I should clarify.

  1. I control both servers that I’m working with, so the security risk
    is practically nil.
  2. The app is very small. Any performance hits I’d take by including a
    remote file is acceptable.

That said, still don’t have a fix.

Thanx.

Eric

Eric G. wrote:

If there’s some sort of risk, please inform.

I agree, this is a terrible idea from both performance and architectural
perspectives. Why do you want your navbar to live on another server?

Not that it matters, that didn’t work either.

What happened when you tried the open().read solution? Just saying that
it didn’t work isn’t that helpful.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

What happened was I got the “We’re sorry, but something went wrong.”
page from my
app.

I would prefer it all to be on one server, but the company I’m trying
to do this for has many different projects, using different languages
on two different servers. I’d rather not include a copy on the second
server, so if I have to make a change to that one file (remember kids,
don’t repeat yourself!) I have to only do it once on one server.

On Sep 30, 11:56 am, Marnen Laibow-Koser <rails-mailing-l…@andreas-

What happened was I got the “We’re sorry, but something went wrong.”
page from my
app.

I would prefer it all to be on one server, but the company I’m trying
to do this for has many different projects, using different languages
on two different servers. I’d rather not include a copy on the second
server, so if I have to make a change to that one file (remember kids,
don’t repeat yourself!) I have to only do it once on one server.

On Sep 30, 11:56 am, Marnen Laibow-Koser <rails-mailing-l…@andreas-

Eric G. wrote:

What happened was I got the “We’re sorry, but something went wrong.”
page from my
app.

Yes, that’s all you will get on the screen in dev mode. What’s in the
log?

I would prefer it all to be on one server, but the company I’m trying
to do this for has many different projects, using different languages
on two different servers. I’d rather not include a copy on the second
server, so if I have to make a change to that one file (remember kids,
don’t repeat yourself!) I have to only do it once on one server.

If you can’t set up a caching mechanism for the navbar, then this would
be one of a very few cases in which I might actually recommend an
iframe. But let me see if I can come up with an answer to your original
question…

On Sep 30, 11:56�am, Marnen Laibow-Koser <rails-mailing-l…@andreas-

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Right on, that worked for me. Thanx so much!

You must require ‘open-uri’ to be able to open URLS. For example:

open(“http://example.com/”).read
Errno::ENOENT: No such file or directory - http://example.com/
from (irb):2:in initialize' from (irb):2:in open’
from (irb):2

But if you require ‘open-uri’:

require ‘open-uri’
=> true
open(“http://example.com/”).read
=> “\r\n\r\n Example Web Page\r\n \r
\n \r\n

You have reached this web page by
typing"example.com",\r\n"example.net",\r\n or
"example.org" into your web browser.

\r\n

These domain
names are reserved for use in documentation and are not available \r
\n for registration. See RFC \r\n 2606, Section 3.

\r\n\r\n\r
\n\r\n”

On Wed, Sep 30, 2009 at 7:08 PM, Robert W.
[email protected] wrote:

Wow! This is a bad idea. Including a full page inside another page just
makes my skin crawl. If I absolutely had to do something like this I
would maybe grab the page using open(…), but then strip out only the
contents inside the body tag and render that inside the final page.
Otherwise you’ll never be able to get a clean validation anything using
that “navbar page.”

And think of how b0rken the stats will be for the server hosting the
navbar.html. That’s enough to make wanna vomit. quiver


Greg D.
http://destiney.com/

2009/10/1 Robert W. [email protected]:

typing"example.com",\r\n"example.net",\r\n  or
Otherwise you’ll never be able to get a clean validation anything using
that “navbar page.”

I don’t believe the OP ever said that it would be a full page that he
would be including. He is just trying to access partials from one of
his servers on another. Presumably some of the data for the remote
one is not accessible to the local one.

Colin

Mauricio Szabo wrote:

But if you require ‘open-uri’:

require ‘open-uri’
=> true
open(“http://example.com/”).read
=> “\r\n\r\n Example Web Page\r\n \r
\n \r\n

You have reached this web page by
typing"example.com",\r\n"example.net",\r\n or
"example.org" into your web browser.

\r\n

These domain
names are reserved for use in documentation and are not available \r
\n for registration. See RFC \r\n 2606, Section 3.

\r\n\r\n\r
\n\r\n”

Wow! This is a bad idea. Including a full page inside another page just
makes my skin crawl. If I absolutely had to do something like this I
would maybe grab the page using open(…), but then strip out only the
contents inside the body tag and render that inside the final page.
Otherwise you’ll never be able to get a clean validation anything using
that “navbar page.”