# Passing a variable to a partial

**URL:** <https://www.ruby-forum.com/t/passing-a-variable-to-a-partial/123042>\
**Category:** Rails\
**Created:** [December 13, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/passing-a-variable-to-a-partial/123042 "2007-12-13T16:52:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 13, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/passing-a-variable-to-a-partial/123042/1 "2007-12-13T16:52:39Z")

</div>

I have a chunk of rhtml that gets reused by several views in several  
controllers. I make partial out of it \_foo and call it with

\<%= render :partial =\>‘bar/foo’ %\>

# RAILS\_ROOT/app/views/bar/\_foo.rhtml

(note I’m not calling with a collection.) Is there a way to send a  
variable to the partial? the doc says i can use :locals =\> { :name =\>  
‘val’ } but what I want to do is alias :name to another variable. I  
can always set some class variable @var and using that inside the  
partial, but I want to make it explicit what it is that I’m passing to  
the partial.

also, is this the way to share something among views? I’m aware of  
layouts but that seems to be for whole page content, rather than  
common snippets.

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 13, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/passing-a-variable-to-a-partial/123042/2 "2007-12-13T17:31:36Z")

</div>

On Dec 13, 2007, at 7:52 AM, rubynuby wrote:

> can always set some class variable @var and using that inside the  
> partial, but I want to make it explicit what it is that I’m passing to  
> the partial.
> 
> also, is this the way to share something among views? I’m aware of  
> layouts but that seems to be for whole page content, rather than  
> common snippets.

Have a look at the methods discussed in this article,  
[http://www.railsdev.ws/blog/3/modular-page-assembly-in-rails/](http://www.railsdev.ws/blog/3/modular-page-assembly-in-rails/)

–  
def gw  
acts\_as\_n00b  
writes\_at(www.railsdev.ws)  
end

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 13, 2007, 7:53pm UTC](https://www.ruby-forum.com/t/passing-a-variable-to-a-partial/123042/3 "2007-12-13T19:53:14Z")

</div>

On 13 Dec 2007, at 18:15, Phillip K. wrote:

> > end
> 
> As usual, you’re on the ball Greg. I was thinking about that very  
> resource, but couldn’t remember where I saw it.

Well it would be surprising if he’d forgotton something he’d written  
himself 🙂

Fred

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 13, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/passing-a-variable-to-a-partial/123042/4 "2007-12-13T19:17:19Z")

</div>

On Dec 13, 2007, at 10:30 AM, Greg W. wrote:

> Have a look at the methods discussed in this article,  
> [WEBSITE.WS - Your Internet Address For Life™](http://www.railsdev.ws/blog/3/modular-page-assembly-in-rails/)
> 
> –  
> def gw  
> acts\_as\_n00b  
> writes\_at(www.railsdev.ws)  
> end

As usual, you’re on the ball Greg. I was thinking about that very  
resource, but couldn’t remember where I saw it.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 13, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/passing-a-variable-to-a-partial/123042/5 "2007-12-13T20:25:53Z")

</div>

On Dec 13, 2007, at 12:52 PM, Frederick C. wrote:

> > > –  
> > > Well it would be surprising if he’d forgotton something he’d written  
> > > himself 🙂
> 
> Fred
> 
> >

Aye, and you are on the ball, too, Fred. 🙂

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 13, 2007, 11:13pm UTC](https://www.ruby-forum.com/t/passing-a-variable-to-a-partial/123042/6 "2007-12-13T23:13:47Z")

</div>

thank you. excellent article.

an O/T question. what is the font used in the pdf for the code  
portion? it’s a nice fixed monospace font and I’m always on the  
lookout for good monospace fonts to use for coding 🙂 Currently I use  
Lucida Console but it has a problem in that commas and periods look  
too much alike.

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 14, 2007, 12:17am UTC](https://www.ruby-forum.com/t/passing-a-variable-to-a-partial/123042/7 "2007-12-14T00:17:08Z")

</div>

On Dec 13, 2007, at 10:52 AM, Frederick C. wrote:

> himself 🙂  
> Well, given that I tend to write such things at the moment I reach my  
> epiphany about the topic, I can very easily forget what I write 🙂  
> I’ve been pointed to my own advice more than once 😛

–  
def gw  
acts\_as\_n00b  
writes\_at(www.railsdev.ws)  
end

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [December 14, 2007, 12:17am UTC](https://www.ruby-forum.com/t/passing-a-variable-to-a-partial/123042/8 "2007-12-14T00:17:44Z")

</div>

On Dec 13, 2007, at 2:13 PM, rubynuby wrote:

> an O/T question. what is the font used in the pdf for the code  
> portion?

That would be Monaco (default monospaced font on a Mac)

– gw
