# How to retrive multiple vaules from a view for a text\_field!

**URL:** <https://www.ruby-forum.com/t/how-to-retrive-multiple-vaules-from-a-view-for-a-text-field/153034>\
**Category:** Rails\
**Created:** [November 11, 2008, 10:46am UTC](https://www.ruby-forum.com/t/how-to-retrive-multiple-vaules-from-a-view-for-a-text-field/153034 "2008-11-11T10:46:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![raji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb8c5e/32.png) [@raji](https://www.ruby-forum.com/u/raji)\
**Post date:** [November 11, 2008, 10:46am UTC](https://www.ruby-forum.com/t/how-to-retrive-multiple-vaules-from-a-view-for-a-text-field/153034/1 "2008-11-11T10:46:43Z")

</div>

Hi,

I have view page which takes all the information like contact’s emails ,  
phones no’s etc and saves different tables. I want to save more than one  
email id’s for a particular contact.

view page is something like this…

.  
.  
Email 1 :  
\<%= select ‘email’, ‘email\_type’, %w(Business Personal), :include\_blank  
=\> false %\>  
\<%= text\_field ‘email’,‘email’,:size=\>“35”,:maxlength=\>“80” %\>  
\<%= radio\_button ‘email’, ‘isprimary’, ‘1’ %\>Primary  
Email 2 :  
\<%= select ‘email’, ‘email\_type’, %w(Business Personal), :include\_blank  
=\> false %\>  
\<%= text\_field ‘email’,‘email’,:size=\>“35”,:maxlength=\>“80” %\>  
\<%= radio\_button ‘email’, ‘isprimary’, ‘1’ %\>Primary  
.  
.

Controller is :

def create  
@contact = Contact.new(params[:contact])  
@contact.emails \<\< Email.new(params[:email])  
@contact.phones \<\< Phone.new(params[:phone])  
@contact.addresses \<\< Address.new(params[:address])  
puts “hellooooooo bharati”  
params.inspect  
if @contact.save  
flash[:notice] = “Contact was successfully created.”  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
flash[:notice] = “Contact was not created.”  
end  
end

Can any one help me how to send more than one email ids to controller…

---

<div class="post-metadata">

**Author:** ![raji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb8c5e/32.png) [@raji](https://www.ruby-forum.com/u/raji)\
**Post date:** [November 11, 2008, 11:20am UTC](https://www.ruby-forum.com/t/how-to-retrive-multiple-vaules-from-a-view-for-a-text-field/153034/2 "2008-11-11T11:20:43Z")

</div>

On Nov 11, 9:46 am, Raji M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,
> 
> I have view page which takes all the information like contact’s emails ,  
> phones no’s etc and saves different tables. I want to save more than one  
> email id’s for a particular contact.
> 
> view page is something like this…

There’s two separate problems here:

- how you manage the ui, ie adding extra textfields to the page
- how you convince rails to treat these textfields as an array.

The key to the second part is how you name the parameters. In  
particular parameters ending with a [] are treated as arrays.

Fred

---

<div class="post-metadata">

**Author:** ![raji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb8c5e/32.png) [@raji](https://www.ruby-forum.com/u/raji)\
**Post date:** [November 12, 2008, 6:18am UTC](https://www.ruby-forum.com/t/how-to-retrive-multiple-vaules-from-a-view-for-a-text-field/153034/3 "2008-11-12T06:18:05Z")

</div>

Frederick C. wrote:

> On Nov 11, 9:46ï¿½am, Raji M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Hi,
> > 
> > I have view page which takes all the information like contact’s emails ,  
> > phones no’s etc and saves different tables. I want to save more than one  
> > email id’s for a particular contact.
> > 
> > view page is something like this…
> 
> There’s two separate problems here:
> 
> - how you manage the ui, ie adding extra textfields to the page
> - how you convince rails to treat these textfields as an array.
> 
> The key to the second part is how you name the parameters. In  
> particular parameters ending with a [] are treated as arrays.
> 
> Fred

Yes . I was able to submit more than one text field values… but now my  
issue is how do i differentiate radio buttons with the same name… both  
the radio buttons are getting selected :(… here is the code

\<% form\_for :@contact do |f| %\>  
\<% @contact.emails.each\_with\_index do |email, index| %\>  
\<% fields\_for “email[#{index}]”, email do |f| %\>   
Email &nbsp &nbsp;  
\<%= f.select :email\_type, %w(Business Personal),  
:include\_blank =\> false %\>  
\<%= f.text\_field :email %\>  
\<%= f.radio\_button :isprimary ,‘index’ %\>Primary  
  
\<% end %\>  
\<% end %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![raji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb8c5e/32.png) [@raji](https://www.ruby-forum.com/u/raji)\
**Post date:** [November 12, 2008, 10:32am UTC](https://www.ruby-forum.com/t/how-to-retrive-multiple-vaules-from-a-view-for-a-text-field/153034/4 "2008-11-12T10:32:38Z")

</div>

On Nov 12, 5:18 am, Raji M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> issue is how do i differentiate radio buttons with the same name… both  
> the radio buttons are getting selected :(… here is the code

the builders yielded by form\_for and fields\_for have the same name, so  
they’ll be overwriting each other (which isn’t what I thing you want).  
Also shouldn’t the second parameter to radio\_button be index and not  
‘index’ ?

Fred

---

<div class="post-metadata">

**Author:** ![raji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb8c5e/32.png) [@raji](https://www.ruby-forum.com/u/raji)\
**Post date:** [November 12, 2008, 11:33am UTC](https://www.ruby-forum.com/t/how-to-retrive-multiple-vaules-from-a-view-for-a-text-field/153034/5 "2008-11-12T11:33:06Z")

</div>

On 12 Nov 2008, at 10:13, Raji M. wrote:

> > so  
> > where as the other values get saved if i do not select radio button.

From the code I’d guess that you end up with params[:email]  
containing the string 1, which you then try and call values on.

Fred

---

<div class="post-metadata">

**Author:** ![raji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb8c5e/32.png) [@raji](https://www.ruby-forum.com/u/raji)\
**Post date:** [November 12, 2008, 11:13am UTC](https://www.ruby-forum.com/t/how-to-retrive-multiple-vaules-from-a-view-for-a-text-field/153034/6 "2008-11-12T11:13:27Z")

</div>

Frederick C. wrote:

> On Nov 12, 5:18ï¿½am, Raji M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > issue is how do i differentiate radio buttons with the same name… both  
> > the radio buttons are getting selected :(… here is the code
> 
> the builders yielded by form\_for and fields\_for have the same name, so  
> they’ll be overwriting each other (which isn’t what I thing you want).  
> Also shouldn’t the second parameter to radio\_button be index and not  
> ‘index’ ?
> 
> Fred

ya. got it. But there exists an issues… When i select radio button i ll  
get the following error. . “undefined method `values’ for “1”:String”  
where as the other values get saved if i do not select radio button.

Also If i enter only one email id the form is not getting saved.  
Controller is as follows.

def new  
@contact = Contact.new  
2.times { @contact.emails.build }  
end  
def create  
@contact = Contact.new(params[:contact])  
params[:email].each\_value do |email|  
@contact.emails.build(email) unless email.values.all?(&:blank?)  
end  
if @contact.save  
flash[:notice] = “Successfully created new Contact.!!!”  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

Where i went wrong?
