# Using partials in a for loop

**URL:** <https://www.ruby-forum.com/t/using-partials-in-a-for-loop/60187>\
**Category:** Rails\
**Created:** [May 11, 2006, 12:05am UTC](https://www.ruby-forum.com/t/using-partials-in-a-for-loop/60187 "2006-05-11T00:05:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [May 11, 2006, 12:05am UTC](https://www.ruby-forum.com/t/using-partials-in-a-for-loop/60187/1 "2006-05-11T00:05:47Z")

</div>

I’m trying to write a partial for a link\_to\_remote that i’m using over  
and over again, but simply moving the code to a partial doesn’t seem to  
work.

Here is how I have it set up:

class\_planner.rhtml:

\<% for course in @courses %\>  
\<%= render :partial =\> “class\_list” %\>  
\<% end %\>

\_class\_list.rhtml:

- \<%= link\_to\_remote "#{course.title}", {:url =\> {:action =\> "add\_course", :id =\> course}}, :class =\> "course", :name =\> "#{course.title}" %\>

The main error I’m getting is that the method ‘course’ is not found.

Is there something that I have to do differently when using a partial?  
I’m thinking that I can’t access the ‘course’ using #{course.whatever}  
in a partial, but I’m not sure how to access it.

Any help would be much appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [May 11, 2006, 12:13am UTC](https://www.ruby-forum.com/t/using-partials-in-a-for-loop/60187/2 "2006-05-11T00:13:15Z")

</div>

Your problem is that course isn’t in the scope of the partial. By  
default, the only variable that will get automatically passed is an @  
variable that has the same name as the partial itself. If you want to  
pass course into the partial, you need to amend your render call:

render(:partial =\> “course\_list”, :locals =\> {:course =\> course})

And then, in your partial, @course should be available.

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [May 11, 2006, 12:14am UTC](https://www.ruby-forum.com/t/using-partials-in-a-for-loop/60187/3 "2006-05-11T00:14:21Z")

</div>

Ben wrote:

> I’m trying to write a partial for a link\_to\_remote that i’m using over  
> and over again, but simply moving the code to a partial doesn’t seem to  
> work.
> 
> Here is how I have it set up:
> 
> class\_planner.rhtml:
> 
> \<% for course in @courses %\>  
> \<%= render :partial =\> “class\_list” %\>  
> \<% end %\>
> 
> \_class\_list.rhtml:
> 
> - \<%= link\_to\_remote "#{course.title}", {:url =\> {:action =\> "add\_course", :id =\> course}}, :class =\> "course", :name =\> "#{course.title}" %\>
> 
> The main error I’m getting is that the method ‘course’ is not found.
> 
> Is there something that I have to do differently when using a partial?  
> I’m thinking that I can’t access the ‘course’ using #{course.whatever}  
> in a partial, but I’m not sure how to access it.
> 
> Any help would be much appreciated.
> 
> Thanks.

Your local variable you set up before you call the partial are out of  
scope, and do not exist within the partial. Instance would exist, but  
you shouldn’t set instance variables in your view. The right is to tell  
partial to provide values for the local variables.

\<% for course in @courses %\>  
\<%= render :partial =\> “class\_list”, :locals =\> {:course =\> course} %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [May 11, 2006, 12:16am UTC](https://www.ruby-forum.com/t/using-partials-in-a-for-loop/60187/4 "2006-05-11T00:16:52Z")

</div>

Bryan D. wrote:

> Your problem is that course isn’t in the scope of the partial. By  
> default, the only variable that will get automatically passed is an @  
> variable that has the same name as the partial itself. If you want to  
> pass course into the partial, you need to amend your render call:
> 
> render(:partial =\> “course\_list”, :locals =\> {:course =\> course})
> 
> And then, in your partial, @course should be available.

local vars are created not instance vars, so you access it with ‘course’  
not ‘@course’

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [May 11, 2006, 1:50am UTC](https://www.ruby-forum.com/t/using-partials-in-a-for-loop/60187/5 "2006-05-11T01:50:27Z")

</div>

Thanks guys, that got it going.
