# Includes: The Ruby Way?

**URL:** <https://www.ruby-forum.com/t/includes-the-ruby-way/77427>\
**Category:** Rails\
**Created:** [November 27, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/includes-the-ruby-way/77427 "2006-11-27T23:00:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [November 27, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/includes-the-ruby-way/77427/1 "2006-11-27T23:00:58Z")

</div>

I was wondering if anyone had an opinion on how to do file includes in  
RoR. I am currently doing it with JavaScript.

I am building a CMS for a dynamic website. I want a tool that lets  
non-programmers build a website by filling out some forms, making some  
choices (templates, images, etc…), and previewing the page before  
publishing it.

The tool should take the info from the CMS forms and populate modules  
(mini web pages) that will then make-up the website. I plan on building  
the final webpage by including the modules in the template.

So anyways, what’s the Ruby way to do includes?

Code and pointers to tutorials are always helpful.

Thanks in Advance – K

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [November 28, 2006, 2:37am UTC](https://www.ruby-forum.com/t/includes-the-ruby-way/77427/2 "2006-11-28T02:37:26Z")

</div>

The very same thing that bothers me in ruby. We can do this “include”  
very  
easily in PHP. And I can’t seem to find the same method to “include” in  
ruby.

As for rails, maybe you can try using render\_component, like me. I use  
render\_component to include modules (as in joomla) in pages. Or using  
helpers to generate components and modules (as in joomla).

I admit that this seem kind of messy… But I can’t think of any other  
way  
right now… ~\_~

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [November 28, 2006, 4:13am UTC](https://www.ruby-forum.com/t/includes-the-ruby-way/77427/3 "2006-11-28T04:13:40Z")

</div>

render :partial =\> “path/to/partial”

Remember, partials can take in variables as well, or can simply act like  
includes and use variables declared in the request scope (@ variables)

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [November 28, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/includes-the-ruby-way/77427/4 "2006-11-28T13:00:44Z")

</div>

> > The very same thing that bothers me in ruby. We can do this “include” very  
> > easily in PHP. And I can’t seem to find the same method to “include” in  
> > ruby.

Bear in mind that PHP’s include functionality has two purposes - 1)  
including other source code - the Ruby equivalent of this is require.  
2) Including templates within templates (like server-side includes) -  
the reason PHP makes this easy is because it was originally designed as  
a templating language. Ruby wasn’t therefore the whole concept of SSIs  
in Ruby wouldn’t make sense. In Rails, this is what partials are for.

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [November 28, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/includes-the-ruby-way/77427/5 "2006-11-28T21:12:32Z")

</div>

Thanks everyone. Partials work great.

–K

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 14, 2007, 4:03pm UTC](https://www.ruby-forum.com/t/includes-the-ruby-way/77427/6 "2007-12-14T16:03:22Z")

</div>

I need to include an outside resource. Is it possible to do with render  
or is there something else that will work?

\<%= render :partial =\> “[http://domain.com:6969/files/](http://domain.com:6969/files/)” %\>

Thanks

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 14, 2007, 4:56pm UTC](https://www.ruby-forum.com/t/includes-the-ruby-way/77427/7 "2007-12-14T16:56:43Z")

</div>

On Fri, Dec 14, 2007 at 04:03:22PM +0100, Peter H. wrote:

> I need to include an outside resource. Is it possible to do with render  
> or is there something else that will work?
> 
> \<%= render :partial =\> “[http://domain.com:6969/files/](http://domain.com:6969/files/)” %\>

There are two plausible ways to go about this:

You’ll need to require ‘open-uri’ for this first one, which has the  
Rails  
app connect to and read from the URL:

\<%= open(“[http://domain.com:6969/files/](http://domain.com:6969/files/)”, &:read) %\>

The other way is to use an HTML iframe (and you’ll need to check the  
syntax  
on that). This makes the client browser request the URL on its own:

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 14, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/includes-the-ruby-way/77427/8 "2007-12-14T17:54:30Z")

</div>

Gregory S. wrote:

> On Fri, Dec 14, 2007 at 04:03:22PM +0100, Peter H. wrote:
> 
> > I need to include an outside resource. Is it possible to do with render  
> > or is there something else that will work?
> > 
> > \<%= render :partial =\> “[http://domain.com:6969/files/](http://domain.com:6969/files/)” %\>
> 
> There are two plausible ways to go about this:
> 
> You’ll need to require ‘open-uri’ for this first one, which has the  
> Rails  
> app connect to and read from the URL:
> 
> \<%= open(“[http://domain.com:6969/files/](http://domain.com:6969/files/)”, &:read) %\>
> 
> The other way is to use an HTML iframe (and you’ll need to check the  
> syntax  
> on that). This makes the client browser request the URL on its own:

Many thanks Greg  
I was using the iframe as a stop gap but IE does not display it  
properly. Your first suggestion works great!!

Thanks again,  
Peter
