# Partial variable help

**URL:** <https://www.ruby-forum.com/t/partial-variable-help/89957>\
**Category:** Rails\
**Created:** [March 29, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/partial-variable-help/89957 "2006-03-29T15:35:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 29, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/partial-variable-help/89957/1 "2006-03-29T15:35:26Z")

</div>

Can’t work this out:

In list.rhtml:  
\<% for employee in @employees -%\>

\<%= render(:partial =\> “list\_form”, :object =\> employee) %\>

\<% end -%\>

In \_list\_form.rhtml (partial):  
\<%= employee.id %\>

In controller:  
@employees = Employee.find(:all)

I get the following error:  
undefined local variable or method `employee’

I everything is correct as far as I can see, just no variables are  
getting through?

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 29, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/partial-variable-help/89957/2 "2006-03-29T16:24:20Z")

</div>

James W. wrote:

> Can’t work this out:
> 
> In list.rhtml:  
> \<% for employee in @employees -%\>
> 
> \<%= render(:partial =\> “list\_form”, :object =\> employee) %\>
> 
> \<% end -%\>
> 
> In \_list\_form.rhtml (partial):  
> \<%= employee.id %\>
> 
> In controller:  
> @employees = Employee.find(:all)
> 
> I get the following error:  
> undefined local variable or method `employee’
> 
> I everything is correct as far as I can see, just no variables are  
> getting through?

From Rails API Docs:

Rendering partials  
Partial rendering is most commonly used together with Ajax calls that  
only update one or a few elements on a page without reloading. Rendering  
of partials from the controller makes it possible to use the same  
partial template in both the full-page rendering (by calling it from  
within the template) and when sub-page updates happen (from the  
controller action responding to Ajax calls). By default, the current  
layout is not used.

# Renders a collection of the same partial by making each element of

@wins available through

# the local variable “win” as it builds the complete response

render :partial =\> “win”, :collection =\> @wins

E.g From my app:

list.rhtml  
\<% if !@contacts.empty? %\>  
\<%= render :partial =\> ‘contacts’, :collection =\> @contacts, :locals  
=\> { :hidden =\> false } %\>  
\<% end %\>

\_contacts.html  
\<%= contact.name %\>

Hope this helps

/Joseph

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 29, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/partial-variable-help/89957/3 "2006-03-29T16:34:28Z")

</div>

I believe that when you send a variable to a partial with the :object  
argument, it takes on the name of the partial.

In your example, that would mean the variable would be called  
“list\_form”.

If all else fails, you can use the :locals argument, like so:

render(:partial =\> “list\_form”, :locals =\> { :employee =\> employee })

Jeff

James W. wrote:

> Can’t work this out:
> 
> In list.rhtml:  
> \<% for employee in @employees -%\>
> 
> \<%= render(:partial =\> “list\_form”, :object =\> employee) %\>
> 
> \<% end -%\>
> 
> In \_list\_form.rhtml (partial):  
> \<%= employee.id %\>
> 
> In controller:  
> @employees = Employee.find(:all)
> 
> I get the following error:  
> undefined local variable or method `employee’
> 
> I everything is correct as far as I can see, just no variables are  
> getting through?

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 29, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/partial-variable-help/89957/4 "2006-03-29T17:06:06Z")

</div>

Jeff C.man wrote:

> I believe that when you send a variable to a partial with the :object  
> argument, it takes on the name of the partial.
> 
> In your example, that would mean the variable would be called  
> “list\_form”.
> 
> If all else fails, you can use the :locals argument, like so:
> 
> render(:partial =\> “list\_form”, :locals =\> { :employee =\> employee })
> 
> Jeff
> 
> James W. wrote:
> 
> > Can’t work this out:
> > 
> > In list.rhtml:  
> > \<% for employee in @employees -%\>
> > 
> > \<%= render(:partial =\> “list\_form”, :object =\> employee) %\>
> > 
> > \<% end -%\>
> > 
> > In \_list\_form.rhtml (partial):  
> > \<%= employee.id %\>
> > 
> > In controller:  
> > @employees = Employee.find(:all)
> > 
> > I get the following error:  
> > undefined local variable or method `employee’
> > 
> > I everything is correct as far as I can see, just no variables are  
> > getting through?

Yes I have just realised that it takes the name of the partial as the  
local variable name.

I have ran into another issue. I am using link\_to\_remote to update a  
form via ajax. However I need it to replace the div with a partial.  
However when pressed it jumps me to a blank screen? no error or  
anything.
