# List \> partial \> edit

**URL:** <https://www.ruby-forum.com/t/list-partial-edit/145854>\
**Category:** Rails\
**Created:** [August 14, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854 "2008-08-14T16:05:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/1 "2008-08-14T16:05:54Z")

</div>

HI, i have a simple list of users, underneath each one i want to display  
the corresponding edit view via a partial.  
BUt, it always displays the person no1 in the edit view…

Person1  
…edit(partial)

> Person1(should be 1)  
> Person2  
> Preson1(should be 1)  
> Person3  
> Person1(should be 1)  
> Person4  
> …

whats wrong here? im not passing locals to the partial…

thx

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 4:13pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/2 "2008-08-14T16:13:05Z")

</div>

On 14 Aug 2008, at 15:05, Tom T. wrote:

> > Preson1(should be 1)  
> > Person3  
> > Person1(should be 1)  
> > Person4  
> > …
> 
> whats wrong here? im not passing locals to the partial…

What are you doing?

Fred

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/3 "2008-08-14T16:23:01Z")

</div>

Frederick C. wrote:

> On 14 Aug 2008, at 15:05, Tom T. wrote:
> 
> > > Preson1(should be 1)  
> > > Person3  
> > > Person1(should be 1)  
> > > Person4  
> > > …
> > 
> > whats wrong here? im not passing locals to the partial…
> 
> What are you doing?
> 
> Fred

woops, myu mistake: should be:

Person3

> Edit Person3  
> Person4  
> Edit Person4  
> …  
> …

but the edit view is always Person1

…

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/4 "2008-08-14T16:45:15Z")

</div>

user/\_edit:  
\<% form\_for(@user) do |f| %\>  
\<%= f.error\_messages %\>  
**Title**  
  
\<%= f.text\_field :username %\>

\<%= f.submit "Update" %\>

\<% end %\> ----------------------------------------- user/index
# Listing Users
 \<% for u in @users %\> \<% end %\>

| Firstname | Lastname | Username |
| --- | --- | --- |
| \<%=h u.firstname + u.id.to\_s %\> | \<%=h u.lastname %\> | \<%=h u.username %\> | \<%= link\_to 'Show', u %\> |
| \<%= render :partial =\>'edit' %\> |

  

gives me the result above…  
thx 4 looking over

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/5 "2008-08-14T16:27:45Z")

</div>

On 14 Aug 2008, at 15:23, Tom T. wrote:

> > > whats wrong here? im not passing locals to the partial…
> > 
> > What are you doing?
> > 
> > Fred

Can’t guess what’s wrong if we can’t see what you’re doing.

Fred

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/6 "2008-08-14T17:51:31Z")

</div>

Frederick C. wrote:

> On 14 Aug 2008, at 15:45, Tom T. wrote:
> 
> > \<% end %\>
> 
> Well, that’s expected - in your partial you always edit @user.  
> if you do render :partial =\> ‘edit’, :object =\> u  
> and then change the partial to be  
> form\_for(object) …
> 
> then you should be ok
> 
> Fred

ii changed it into  
a)  
, :object =\> u  
and  
\<% form\_for(u) do |f| %\>  
b)  
, :object =\> u  
and  
\<% form\_for(u) do |f| %\>

still the same result

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/7 "2008-08-14T18:40:16Z")

</div>

Hey Tom,  
in your index, you have to pass the right variable to the partial,  
like Fred wrote:  
render :partial =\> ‘edit’, :user =\> u

u is the local variable you are passing (you called: for u in @users)  
user is then the name of the variable you will use in your edit  
partial (in Fred’s example, it was called ‘object’).

So in your partial, you need to do:  
\<% form\_for user do %\>

You can call it anything you want, but the symbol you use must match  
the variable name you use in your partial. In your example, you’re  
using ‘u’ but you never passed a variable called ‘u’ to the partial  
(you passed ‘object’).

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/8 "2008-08-14T18:44:23Z")

</div>

On 14 Aug 2008, at 16:51, Tom T. wrote:

> > form\_for(object) …  
> > b)  
> > , :object =\> u  
> > and  
> > \<% form\_for(u) do |f| %\>

that should be form\_for object. The current object is always available  
under two names: the name derived from the partial and object.

Fred

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/9 "2008-08-14T16:56:29Z")

</div>

On 14 Aug 2008, at 15:45, Tom T. wrote:

> \<% end %\>

Well, that’s expected - in your partial you always edit @user.  
if you do render :partial =\> ‘edit’, :object =\> u  
and then change the partial to be  
form\_for(object) …

then you should be ok

Fred

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/10 "2008-08-14T18:50:51Z")

</div>

deegee wrote:

> Hey Tom,  
> in your index, you have to pass the right variable to the partial,  
> like Fred wrote:  
> render :partial =\> ‘edit’, :user =\> u
> 
> u is the local variable you are passing (you called: for u in @users)  
> user is then the name of the variable you will use in your edit  
> partial (in Fred’s example, it was called ‘object’).
> 
> So in your partial, you need to do:  
> \<% form\_for user do %\>
> 
> You can call it anything you want, but the symbol you use must match  
> the variable name you use in your partial. In your example, you’re  
> using ‘u’ but you never passed a variable called ‘u’ to the partial  
> (you passed ‘object’).

mhh, somehow im totally confused now. the index action /view iterates  
through @users with an iterator called u:

# Listing Users
 \<% for u in @users %\> \<% end %\>

| Firstname | Lastname | Username |
| --- | --- | --- |
| \<%=h u.firstname + u.id.to\_s %\> | \<%=h u.lastname %\> | \<%=h u.username %\> | \<%= link\_to 'Show', u %\> |
| \<%= render :partial =\>'edit' , :@user =\> @users %\> |

  
/////////////////////////// user edit controller: def edit @user = User.find(params[:id]) end means i have somehow to use that variable.

////////////////////////////////  
partial:

# Editing User
\<% form\_for(@user) do |f| %\> 

**Title**  
 \<%= f.text\_field :username %\>

\<%= f.submit "Update" %\>

\<% end %\>

…summary: means i could replace

\<%= render :partial =\>‘edit’ , :@user =\> @users %\>  
with  
\<%= render :partial =\>‘edit’ , :@whatever =\> u %\> //u because of the  
iterator

and use @whatever in the edit-display:  
% form\_for(whatever) do |f|  
???  
but that cant be true, where is the @users being used from the EDIT  
action out of the controller?  
i cant see thriough right now. help

thx

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 7:07pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/11 "2008-08-14T19:07:17Z")

</div>

On 14 Aug 2008, at 17:50, Tom T. wrote:

> >

Um, that’s just wrong. :object is an option to render. if you want to  
pass arbitrality named local variables use the locals option

> # Listing Users
> \<%=h u.username %\> def edit \<%= f.text\_field :username %\> 
> 
> \<%= f.submit "Update" %\>
> 
> \<% end %\>

Are you reusing this same partial both from the edit action and from  
the index action ?  
If so then in the edit action you would do  
render :partial =\> ‘edit’, :object =\> @user  
in the list action  
render :partial =\> ‘edit’, :object =\> u

and in the partial itself:  
form\_for object

Fred

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 7:11pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/12 "2008-08-14T19:11:24Z")

</div>

First of all, it shouldn’t be :@whatever but :whatever. You want a  
symbol for the name of the variable you’ll be using in the partial.

Second, it’s your index action in the controller that is calling the  
index view (and also the edit partial). You’re using the @users  
instance variable set by the index action, iterating through each of  
them (creating a variable u) and then calling the partial view,  
passing the variable ‘whatever’ if you use the symbol :whatever.

You edit action in the controller is not needed anymore, unless you  
want to call it in another context. It requires a view edit (that is  
not your partial), which of course could call the same partial using  
render :partial =\> edit, :whatever =\> @user  
see, here you’re using the @user from the edit action, because that’s  
the action that called the view.

Hope that helps.

Dirk.

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/13 "2008-08-14T19:14:55Z")

</div>

Sorry, my mistake, it should be :object =\> @user in the edit view,  
and :object =\> u in the index view. I was thinking of locals indeed.  
Would work as well 🙂

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 7:32pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/14 "2008-08-14T19:32:22Z")

</div>

deegee wrote:

> Sorry, my mistake, it should be :object =\> @user in the edit view,  
> and :object =\> u in the index view. I was thinking of locals indeed.  
> Would work as well 🙂

thx guys - ill try to get this done.  
please stay subscribed.thx tom

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/15 "2008-08-14T19:51:41Z")

</div>

ok i tried a few things, and it seems that everything depends on two  
lines:

 \<%= render :partial =\>'edit', :object=\>@users %\>

and  
…"\_edit"…  
\<% form\_for :user, @users do |f| %\>

**Title**  
 \<%= f.text\_field :username %\> ....

facts:  
yes i copied the origin edit to \_edit because there a minor differences.  
here i would like to use the partial “\_edit”. i can see and understand  
that the edit action out of the user controller wont be triggered,  
though i havent read the api for that yet.  
so, so far i understood what u tried to xplain is: regardless what i  
pass into the partial, i have to reuse that name because it appears  
within the “scope” od that function / loop (one output).  
i dont understand why i should pass the list-object called @object  
because that contains for my understanding all users returned by the  
list-action-controller. anyhow i tried to pass on the local variable u  
and neither that worked because it couldnt find the attributes later  
like here: “f.text\_field :username”. the few times the server doesnt  
throw an exception is when the result list gives me always the same name  
in the edit partial…  
does anyone have a working example? or can reproduce it?  
thx

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 7:29pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/16 "2008-08-14T19:29:00Z")

</div>

Here are the two variants from the rdoc of actionview, I was thinking  
of the first one but forgot to add the :locals part, sorry about that.

# Renders the same partial with a local variable.

render :partial =\> “person”, :locals =\> { :name =\> “david” }

# Renders the partial, making @new\_person available through

# the local variable ‘person’

render :partial =\> “person”, :object =\> @new\_person

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/17 "2008-08-14T21:02:18Z")

</div>

On Aug 14, 6:51 pm, Tom T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> <%= f.text_field :username %>
> 
> ```
> 
> …

I’ll try again. What you need to do to reuse the partial is decouple  
it from whatever instance variables are used. That means that @ signs  
are banished from your partial. use the local variable the partial  
creates for you (object or the name of the partial).  
when you render a partial, you can specify what that object should be  
with the :object option. That’s all there is to it.

You’ll never need :object =\> @users.

Fred

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/18 "2008-08-14T21:19:21Z")

</div>

Frederick C. wrote:

> On Aug 14, 6:51ï¿½pm, Tom T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ ï¿½ \<%= f.text\_field :username %\>  
> > …
> 
> I’ll try again. What you need to do to reuse the partial is decouple  
> it from whatever instance variables are used. That means that @ signs  
> are banished from your partial. use the local variable the partial  
> creates for you (object or the name of the partial).  
> when you render a partial, you can specify what that object should be  
> with the :object option. That’s all there is to it.
> 
> You’ll never need :object =\> @users.
> 
> Fred

fred, im really sorry, but im missing her something. when does the  
partial create a variable except the local variable f?  
could u give em an example base don my last post? that would be help  
ful, sas well if u could use complete different names for the variables/

thx u so much!

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 9:24pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/19 "2008-08-14T21:24:00Z")

</div>

> fred, im really sorry, but im missing her something. when does the  
> partial create a variable except the local variable f?

if you do render :partial =\> ‘something’, :object =\> @foo

then in the context of the partial there will be two local variables:  
something and object, which will have the same value as @foo.

Fred

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [August 14, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/list-partial-edit/145854/20 "2008-08-14T22:11:33Z")

</div>

On Aug 14, 8:48 pm, Tom T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hi,
> 
> mh:
> 
> \<%= render :partial =\>'edit', :var=\>u %\>

That is not supposed to work. You have to say :object =\> u. :object is  
the name of an option, not something you can make up your self.  
Similarly what you get in your partial will always be the name of the  
partial and object.

Fred

[Next page](https://www.ruby-forum.com/t/list-partial-edit/145854.md?page=2)
