# Editing N instances of a model on one page

**URL:** <https://www.ruby-forum.com/t/editing-n-instances-of-a-model-on-one-page/52567>\
**Category:** Rails\
**Created:** [January 16, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/editing-n-instances-of-a-model-on-one-page/52567 "2006-01-16T17:10:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [January 16, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/editing-n-instances-of-a-model-on-one-page/52567/1 "2006-01-16T17:10:11Z")

</div>

I have a situation where I want to edit an arbitrary number of instances  
of one model on one page. I can and have accomplished this by manually  
managing the object \<-\> text field relationship, but in a way I consider  
at best hackish. I’d like to know a clean way to do this.

If you want to edit the name attribute on a person class, you write code  
like this

text\_field(“person”,“name”)

Say I have an array in variable @people. What my mind feels is the  
logical thing to do is

for i in 0…n  
p = “people[” + i.to\_s + “]”  
text\_field(p,“name”)  
end

That might be the “ugly” way to do it but you get the idea. I want to  
put N text fields on the screen, each corresponding to the name  
attribute on a different person object. However, that doesn’t work for  
me.

Does anyone have a good/most-often-used idiom for doing this? Help  
would be appreciated. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [January 16, 2006, 5:43pm UTC](https://www.ruby-forum.com/t/editing-n-instances-of-a-model-on-one-page/52567/2 "2006-01-16T17:43:31Z")

</div>

I have read, but not experimented with 2d arrays for form fields, so  
something like this may work -

for key, person in @people  
text\_field(people[key],“name”)  
end

No guarantees on that, but worth investigating a bit further.

matt

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [January 16, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/editing-n-instances-of-a-model-on-one-page/52567/3 "2006-01-16T19:08:01Z")

</div>

It’s my understanding that for the text field helper, you put in a  
string representing the name of your object, and rails takes care of the  
mapping. Here, you are putting the actual object in the field, which  
does not work.

Also, I’m not sure what you are referring to with 2D arrays, unless the  
N objects are 1 dimension and the attributes are the second.

Don’t mean to sound rude, just want to get to the bottom of this. I  
find it hard to believe that Rails doesn’t have an idiom to support  
this.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [January 17, 2006, 2:01am UTC](https://www.ruby-forum.com/t/editing-n-instances-of-a-model-on-one-page/52567/4 "2006-01-17T02:01:11Z")

</div>

Nobody else has ever done this? I didn’t feel like this was a very  
uncommon operation.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [January 26, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/editing-n-instances-of-a-model-on-one-page/52567/5 "2006-01-26T16:09:11Z")

</div>

Mike H. wrote:

> It’s my understanding that for the text field helper, you put in a  
> string representing the name of your object, and rails takes care of the  
> mapping. Here, you are putting the actual object in the field, which  
> does not work.
> 
> Also, I’m not sure what you are referring to with 2D arrays, unless the  
> N objects are 1 dimension and the attributes are the second.
> 
> Don’t mean to sound rude, just want to get to the bottom of this. I  
> find it hard to believe that Rails doesn’t have an idiom to support  
> this.

I, too, am looking for an elegant solution to this.

Without something built-in to text\_field, there’s a lot of manual work  
to do getting the errors indicated properly.

Still searching.

Jake

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [January 26, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/editing-n-instances-of-a-model-on-one-page/52567/6 "2006-01-26T16:26:56Z")

</div>

Check out p356 of the rails book.

\<% for @person in @people%\>  
\<%= text\_field(‘person[]’, ‘name’) %\>  
\<% end %\>

“params[:person] will be a hash of hashes, where each key is the id of  
a model object and the corresponding value are the values from the  
form for that object. In the controller, this could be used to update  
all person rows with something like  
Person.update(params[:person].keys, params[:person].values)”

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [January 26, 2006, 9:23pm UTC](https://www.ruby-forum.com/t/editing-n-instances-of-a-model-on-one-page/52567/7 "2006-01-26T21:23:10Z")

</div>

On 1/26/06, Jake J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > all person rows with something like  
> > performed?

I’ve done this in partials before.  
Assuming the partial is named \_example.rhtml, and the model class is  
Example:  
(This assumes that the partial builds up a table, but that’s not  
required.)

\<% @example = example -%\>  
\<% if @example.errors.count \> 0 then -%\>

 \<%= error\_messages\_for 'example', :id =\> "errorExplanation#{example\_counter}" %\> \<% end -%\> # other table-related gunk goes here. \<%= text\_field 'example', 'something', 'index' =\> example\_counter %\> # end of partial

In the calling view, you’d do:  
\<%= render :partial =\> ‘example’, :collection =\> @examples %\>

render\_partial\_collection sets up the counter variable that prevents  
the error messages from stomping on each other. If any particular  
Example instance has errors, your partial will create a table row for  
it and put the error messages there.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [January 26, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/editing-n-instances-of-a-model-on-one-page/52567/8 "2006-01-26T17:05:30Z")

</div>

Pat M. wrote:

> Check out p356 of the rails book.
> 
> \<% for @person in @people%\>  
> \<%= text\_field(‘person[]’, ‘name’) %\>  
> \<% end %\>
> 
> “params[:person] will be a hash of hashes, where each key is the id of  
> a model object and the corresponding value are the values from the  
> form for that object. In the controller, this could be used to update  
> all person rows with something like  
> Person.update(params[:person].keys, params[:person].values)”

Thanks! That works great. I knew I saw this somewhere, but had a  
really hard time tracking it down again.

Now, when the text\_fields are rendered, it seems they won’t “markup”  
errors in more than one model – or even more than one instance of a  
model. So, I only see errors in one instance at a time.

Is there a way to get it to mark up all instances? Where is this action  
performed?

Jake
