# Multiple text fields all with the same method

**URL:** <https://discuss.rubyonrails.org/t/multiple-text-fields-all-with-the-same-method/3590>\
**Category:** rubyonrails-talk\
**Created:** [October 25, 2006, 5:59pm UTC](https://discuss.rubyonrails.org/t/multiple-text-fields-all-with-the-same-method/3590 "2006-10-25T17:59:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [October 25, 2006, 5:59pm UTC](https://discuss.rubyonrails.org/t/multiple-text-fields-all-with-the-same-method/3590/1 "2006-10-25T17:59:31Z")

</div>

I have 3 text fields in a form , all with the same method, since those params will be inserted via a loop which is set up in the create action. However, I’m noticing that the form is only sending through the 1st value and leaving out the additional 2.

In my form I have this: \<%= text\_field(:cantitle, :title\_opt)%\> \<%= text\_field(:cantitle, :title\_opt)%\> \<%= text\_field(:cantitle, :title\_opt)%\>

Even though I put input into each , the params came through as such:

{“cantitle”=\>{“title\_opt”=\>“CEO”}, # so the 2nd and 3rd were skipped.

In the controller I have this loop- # which if 3 values were submitted , 3 records would be created. a = (params[:cantitle][:title\_opt]) a.each {|x| cantitle = Cantitle.new cantitle.title\_opt = x cantitle.save }

Any ideas how to get each value through ? Also, I’m wondering what would happen if someone filled in the 1st and 3rd box and left the 2nd blank. Do I have to account for a null value ?

Stuart

---

<div class="post-metadata">

**Author:** ![Bala\_Paranj](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@Bala\_Paranj](https://discuss.rubyonrails.org/u/Bala_Paranj)\
**Post date:** [October 25, 2006, 6:15pm UTC](https://discuss.rubyonrails.org/t/multiple-text-fields-all-with-the-same-method/3590/2 "2006-10-25T18:15:07Z")

</div>

It is not possible to have same name for all the fields, that’s why only one field is getting populated.

---

<div class="post-metadata">

**Author:** ![Dark\_Ambient](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dark\_Ambient](https://discuss.rubyonrails.org/u/Dark_Ambient)\
**Post date:** [October 25, 2006, 6:48pm UTC](https://discuss.rubyonrails.org/t/multiple-text-fields-all-with-the-same-method/3590/3 "2006-10-25T18:48:53Z")

</div>

Bala, thanks - you always come through for me. Actually though it is possible (as I just learned) by adding “index” = x

So now I have the text fields as such: \<%= text\_field(:cantitle, :title\_opt, “index” =\> 1)%\>

\<%= text\_field(:cantitle, :title\_opt, “index” =\> 2)%\> \<%= text\_field(:cantitle, :title\_opt, “index” =\> 3)%\>

Which get translated into : \<input id=“canlocation\_1\_city” name=“canlocation[1][city]” size=“30” type=“text” /

> 

And params are such: “cantitle”=\>{“1”=\>{“title\_opt”=\>“CEO”}, “2”=\>{“title\_opt”=\>“COO”}, “3”=\>{“title\_opt”=\>“CIO”}},

Stuart

```ruby

```

---

<div class="post-metadata">

**Author:** ![Bala\_Paranj](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@Bala\_Paranj](https://discuss.rubyonrails.org/u/Bala_Paranj)\
**Post date:** [October 25, 2006, 9:09pm UTC](https://discuss.rubyonrails.org/t/multiple-text-fields-all-with-the-same-method/3590/4 "2006-10-25T21:09:02Z")

</div>

Great, looks like we can learn a lot from the Rails API.
