# Modules / blocks on a page

**URL:** <https://www.ruby-forum.com/t/modules-blocks-on-a-page/89229>\
**Category:** Rails\
**Created:** [March 18, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/modules-blocks-on-a-page/89229 "2007-03-18T23:00:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 18, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/modules-blocks-on-a-page/89229/1 "2007-03-18T23:00:24Z")

</div>

Hi, what’s the best way to display “modules” in a webpage?  
I want to add these little blocks on different pages like a random  
image block for example.

For example the modules on the right on this page:

> **[Radar - O’Reilly](https://www.oreilly.com/radar/)**
>
> Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

What’s the best way to do this? I heared something about partials/  
components?

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 18, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/modules-blocks-on-a-page/89229/2 "2007-03-18T23:04:24Z")

</div>

The Hobo plugin has a feature for specifying layout via blocks which  
are shareable across an application, and lots of other Rails-enhancing  
stuff.

Check it out at:

```
http://www.hobocentral.net

```

It’s really powerful and flexible.

Pd

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 18, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/modules-blocks-on-a-page/89229/3 "2007-03-18T23:59:30Z")

</div>

Thanks. But I’m just beginning with Rails and I’m now making an ultra-  
simple application so I would like to do it myself. I feel that way I  
will understand rails better. So I’m looking for a way I can implement  
that functionality in a simple, standard-rails way.

On Mar 18, 11:03 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 19, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/modules-blocks-on-a-page/89229/4 "2007-03-19T22:40:30Z")

</div>

Anyone else?

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 20, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/modules-blocks-on-a-page/89229/5 "2007-03-20T23:58:39Z")

</div>

Thanks. If I use a partial in my main layout and use an if in the  
partial. Like:  
if (:controller == ‘dashboard’)  
(or something like that)  
would that be considered a hack? Is there a nicer way?

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 21, 2007, 12:49am UTC](https://www.ruby-forum.com/t/modules-blocks-on-a-page/89229/6 "2007-03-21T00:49:05Z")

</div>

No, but there may be a better way of doing it, depending on what you’re  
going to be showing. If the furniture (for want of a better word) is  
substantially different on the dashboard controller, you may be better  
off with separate layout for that controller. Or if the partials are  
dependent on dynamic data, then set that data up in the method called by  
the filter (in my application controller I have before\_filter  
:setup\_layout\_furniture), and do the testing againt the controller in  
the method called. The idea is to keep the views (and layouts) as clean,  
readable, and DRY as poss.

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 22, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/modules-blocks-on-a-page/89229/7 "2007-03-22T21:08:21Z")

</div>

Is it possible to do a :before\_filter that places it’s data in a  
specific div created in my main layout?

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 23, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/modules-blocks-on-a-page/89229/8 "2007-03-23T13:15:33Z")

</div>

The before\_filter just calls the method (which may call other methods),  
which sets up instance variables (which hold the data). So I have:

 \<%= render :partial =\> "menu", :collection =\> @menus %\>

In my case @menus is a collection of Menu instances, but you could  
easily have it as an array of links, strings, or whatever. Hope this  
makes sense,  
Chris

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 20, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/modules-blocks-on-a-page/89229/9 "2007-03-20T20:41:34Z")

</div>

LeonB wrote:

> > wrote:
> > 
> > > It’s really powerful and flexible.
> > > 
> > > > What’s the best way to do this? I heared something about partials/  
> > > > components?
> 
> >

The easy way is to use instance variables to hold the data, perhaps set  
up using filters in the application controller – @menus, @sidebars,  
etc, and then rendering using a partial if they have been set: render  
:partial =\> ‘sidebar’ if @sidebar, for example. It can get more  
sophisticated than this, with a custom class for each type of furniture  
(one for menus, one for sidebars, etc), but hopefully this helps a  
little to start with.  
Chris
