# Can't convert String into Integer error on updating

**URL:** <https://www.ruby-forum.com/t/cant-convert-string-into-integer-error-on-updating/153443>\
**Category:** Rails\
**Created:** [November 17, 2008, 9:57am UTC](https://www.ruby-forum.com/t/cant-convert-string-into-integer-error-on-updating/153443 "2008-11-17T09:57:36Z")\
**Posts on this page:** 2\
**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 17, 2008, 9:57am UTC](https://www.ruby-forum.com/t/cant-convert-string-into-integer-error-on-updating/153443/1 "2008-11-17T09:57:36Z")

</div>

\<% form\_for (:contact,:url=\>{:controller=\>‘contact’,:action =\>  
‘update’,:id =\> @contact},:html=\> {:onSubmit =\> ‘return validate()’, :id  
=\> ‘savecontact’} ) do -%\>  
.  
.  
.  
.

 \<% for email in @contact.emails %\> \<% fields\_for "contact[email\_attributes][]", email do |e| %\> \<% end %\> \<% end %\>

| **Email Addresses** |
| --- |
 | Email | \<%= e.select ('email\_type',%w{Business Personal}, :include\_blank =\> false) %\> \<%= e.text\_field :email,:size=\>"35",:maxlength=\>"80" %\> \<%= e.radio\_button ('isprimary', '1') %\>Primary |

. . is my view page and here is my update function in contact\_cotroller.rb

def update  
@contact = Contact.find(params[:id])  
if @contact.update\_attributes!(params[:contact])  
flash[:notice] = “The contact has been updated successfully.”  
redirect\_to :action =\> ‘view’, :id =\> @contact  
else  
render :action =\> ‘edit’  
end  
end  
is throwing the following error  
can’t convert String into Integer

RAILS\_ROOT: E:/ruby1/Ruby/BCMS  
Application Trace | Framework Trace | Full Trace

app/models/contact.rb:11:in `[]' app/models/contact.rb:11:in`email\_attributes=’  
app/models/contact.rb:10:in `each' app/models/contact.rb:10:in`email\_attributes=’  
app/controllers/contact\_controller.rb:59:in `update

Where am i going wrong…??

and my contact.rb is class Contact \< ActiveRecord::Base  
belongs\_to :company, :counter\_cache =\> false  
has\_many :users  
has\_many :addresses  
has\_many :emails  
has\_many :phones  
validates\_presence\_of :namefirst, :namelast,:message=\>‘Database  
Validation Error from Contact’

def email\_attributes=(email\_attributes)  
email\_attributes.each do |attributes|  
emails.build(attributes) unless attributes[“email”].empty?  
end  
end

plz help

---

<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 17, 2008, 1:43pm UTC](https://www.ruby-forum.com/t/cant-convert-string-into-integer-error-on-updating/153443/2 "2008-11-17T13:43:11Z")

</div>

Hi,  
Just a guess…

On Nov 17, 7:57 pm, Raji M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> color=“fffff”\>Email Addresses
> 
> \<% for email in @contact.emails %\> \<% fields\_for "contact[email\_attributes][]", email do |e| %\>

So this will generate  
params[:contact][:email\_attributes][\<email\_id\>][\<email\_field\>] =\>  
  
Each element in this params is a hash.  
Check your server logs to verify that that is what you’re getting.

> can’t convert String into Integer  
> Where am i going wrong…??  
> def email\_attributes=(email\_attributes)  
> email\_attributes.each do |attributes|

email\_attributes is a hash with keys being email id’s pointing to  
email attributes.  
See previous note above.  
When you do email\_attributes.each |attributes| , ‘attributes’ will be  
an array with the first  
value being the key and the 2nd element being the value.

> ```
> emails.build(attributes) unless attributes["email"].empty?
> 
> ```

Calling attributes[“email”] would trigger the above conversion error  
because arrays expect numeric indexes.

> end  
> end

Also, I noticed you’re saying “:id =\> @contact” . I would have though  
@contact.id but might be missing something.

–  
Daniel B.
