# Error: "Couldn't find Contact without an ID" -\> form\_tag par

**URL:** <https://www.ruby-forum.com/t/error-couldnt-find-contact-without-an-id-form-tag-par/111713>\
**Category:** Rails\
**Created:** [August 10, 2007, 12:44am UTC](https://www.ruby-forum.com/t/error-couldnt-find-contact-without-an-id-form-tag-par/111713 "2007-08-10T00:44:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![svenhecht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@svenhecht](https://www.ruby-forum.com/u/svenhecht)\
**Post date:** [August 10, 2007, 12:44am UTC](https://www.ruby-forum.com/t/error-couldnt-find-contact-without-an-id-form-tag-par/111713/1 "2007-08-10T00:44:14Z")

</div>

Hi,  
I Try to Add to one of my Group objects a Contact object  
My model looks simplified like this:

Group has a Contacts  
Contact has a Group  
Contact has a City  
City is owned by Contacts

I hope thats understandable.

My Form:  
\<% for column in Group.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h @group.send(column.name) %\>

\<% end %\>

\<%= link\_to ‘Edit’, :action =\> ‘edit’, :id =\> @group %\> |  
\<%= link\_to ‘Back’, :action =\> ‘list’ %\>

  

* * *

\<% for contact in @group.contacts %\>  
\<%= contact.first %\>  
  
\<%= contact.name %\>  
  
\<%= contact.adress %\>

* * *
\<% end %\>

\<%= form\_tag :action =\> “contact”, :id =\> @group, : %\>  
First: \<%= text\_field “contact”, “first” %\>  
Last: \<%= text\_field “contact”, “name” %\>  
Adress: \<%= text\_field “contact”, “adress” %\>  
City: \<%= text\_field “contact”, “city\_name” %\>  
\<%= submit\_tag “Add contact!” %\>

in the controller the method looks like this:  
def contact  
newContact = Group.find(params[:id]).contacts.create  
newContact.first = params[:first]  
newContact.name = params[:name]  
newContact.name = params[:adress]  
existingCity = City.find\_on\_conditions(:all, :name =\>  
params[:city\_name])  
newContact.reload

```
if (existingCity[0] != nil) then
  newContact.city = existingCity[0]
else
  newContact.city = City.create
  newContact.city.name = params(:city_name)
end

flash[:notice] = "Added your new contact."
redirect_to :action => "show", :id => params[:id]

```

end

the following ErrorMessage reads like this:  
“Couldn’t find Contact without an ID”  
#{RAILS\_ROOT}/app/controllers/rails\_contacts\_controller.rb:58:in  
`contact’

Its probably a really stupid mistake. I started Ruby 2 days before so  
be gentle 😉  
Thanks in Advance

---

<div class="post-metadata">

**Author:** ![svenhecht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@svenhecht](https://www.ruby-forum.com/u/svenhecht)\
**Post date:** [August 11, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/error-couldnt-find-contact-without-an-id-form-tag-par/111713/2 "2007-08-11T21:00:25Z")

</div>

anyone?

---

<div class="post-metadata">

**Author:** ![svenhecht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@svenhecht](https://www.ruby-forum.com/u/svenhecht)\
**Post date:** [August 17, 2007, 10:30am UTC](https://www.ruby-forum.com/t/error-couldnt-find-contact-without-an-id-form-tag-par/111713/3 "2007-08-17T10:30:18Z")

</div>

I’m a beginner, just like you, but I hope my experience will help.  
After getting the same error message on my code (purely Rails/scaffold-  
generated RHTML for now), I was able to find a few bits of answers on  
various forums. This is what I did (my primary key field is named  
“code”):

In the controller, I replaced all calls to find(params[:id]) with  
calls to find\_by\_code(params[:code]). This function was generated by  
Rails, I guess when I declared [set\_primary\_key “code”] in the model.  
Example:

def show  
#@rv\_wrk\_consult\_type = RvWrkConsultType.find(params[:id])  
# Finding by “CODE”:  
@rv\_wrk\_consult\_type =  
RvWrkConsultType.find\_by\_code(params[:code])  
end

In the views, I replaced “:id” with “:code”. Example:

```
<!-- Replaced ":id" below with ":code" -->
<td><%= link_to 'Show', :action => 'show', :code =>

```

rv\_wrk\_consult\_type %\>

\<%= link\_to ‘Edit’, :action =\> ‘edit’, :code =\>  
rv\_wrk\_consult\_type %\>  
\<%= link\_to ‘Destroy’, { :action =\> ‘destroy’, :code =\>  
rv\_wrk\_consult\_type }, :confirm =\> ‘Are you sure?’, :method =\> :post  
%\>

Finally, I made the following change to \_form.rhtml:

Code  
 \<%= text\_field\_tag 'code', params[:code], :maxlength =\> 10 %\>

I hope this is understandable and can help you out. Of course, all  
these changes are painful, especially if you have tens of legacy  
database tables, like I have; so I suspect I’ll end up creating an  
Oracle view with ID instead of CODE… This should prevent me from  
having to do all these changes… unless the fact that CODE is  
alphanumeric brings up more problems!

Chris.

---

<div class="post-metadata">

**Author:** ![svenhecht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@svenhecht](https://www.ruby-forum.com/u/svenhecht)\
**Post date:** [August 17, 2007, 10:34am UTC](https://www.ruby-forum.com/t/error-couldnt-find-contact-without-an-id-form-tag-par/111713/4 "2007-08-17T10:34:18Z")

</div>

I forgot to tell you about one other modification to the controller.  
Even though my primary key is named “code”, you still need to use “id”  
when you set its value in function create:

```
# Adding the ID ("CODE" in the database), so the non-existant

```

sequence isn’t called:  
@rv\_wrk\_consult\_type.id = params[:code]
