# :locals vs. instance variable in partials ... best practice?

**URL:** <https://www.ruby-forum.com/t/locals-vs-instance-variable-in-partials-best-practice/149860>\
**Category:** Rails\
**Created:** [September 30, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/locals-vs-instance-variable-in-partials-best-practice/149860 "2008-09-30T16:54:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [September 30, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/locals-vs-instance-variable-in-partials-best-practice/149860/1 "2008-09-30T16:54:34Z")

</div>

I’ve been vacillating between using the :locals hash and instance  
variables when passing data to partials. Is there a valid reason to use  
one over the other? What could be considered a best practice in the  
Rails community? I did some googling but didn’t turn anything up.

Peace.

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [September 30, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/locals-vs-instance-variable-in-partials-best-practice/149860/2 "2008-09-30T17:48:17Z")

</div>

On Tue, Sep 30, 2008 at 7:54 AM, Phillip K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve been vacillating between using the :locals hash and instance  
> variables when passing data to partials. Is there a valid reason to use  
> one over the other? What could be considered a best practice in the  
> Rails community? I did some googling but didn’t turn anything up.

As a general rule of thumb, you’ll want to be conservative when it  
comes to scoping your variables. In my opinion, it’s best to use  
locals and not rely on an instance variable.

Cheers,

Robby

–  
Robby R.  
Chief Evangelist, Partner

PLANET ARGON, LLC  
design // development // hosting

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
aim: planetargon

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [September 30, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/locals-vs-instance-variable-in-partials-best-practice/149860/3 "2008-09-30T18:40:04Z")

</div>

In a similar vein…

I’ve always wondered if there’s a performance (or other) difference  
between using an iterating partial (e.g. with :collection =\>) vs. an  
explicit iterator.

For example:  
\<%= render :partial =\> ‘item’, :collection =\> @items %\>

vs.  
\<% @items.each do |i| %\>  
\<%= h i.name %\>  
\<% end %\>

(I suppose there’s also the case of writing an explicit iteration but  
using a partial inside the iteration)

I assume (but haven’t tested) that the latter case is more performant;  
but using the iterating partial feels more ‘rails-ish’ to me…

dwh
