Error: "Couldn't find Contact without an ID" -> form_tag parameter confusion

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 %> <p>   <b><%= column.human_name %>:</b> <%=h @group.send(column.name) %> </p> <% end %>

<%= link_to 'Edit', :action => 'edit', :id => @group %> | <%= link_to 'Back', :action => 'list' %>

<br/> <hr/>

<% for contact in @group.contacts %>   <%= contact.first %><br/>   <%= contact.name %><br/>   <%= contact.adress %>   <hr /> <% end %>

<%= form_tag :action => "contact", :id => @group, : %>   <span>First: </span><%= text_field "contact", "first" %>   <span>Last: </span><%= text_field "contact", "name" %>   <span>Adress: </span><%= text_field "contact", "adress" %>   <span>City: </span><%= text_field "contact", "city_name" %>   <%= submit_tag "Add contact!" %> </form>

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 :wink: Thanks in Advance

anyone?

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 %></td>     <td><%= link_to 'Edit', :action => 'edit', :code => rv_wrk_consult_type %></td>     <td><%= link_to 'Destroy', { :action => 'destroy', :code => rv_wrk_consult_type }, :confirm => 'Are you sure?', :method => :post %></td>

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

<!--[form:rv_wrk_consult_type]--> <p><label for="rv_wrk_consult_type_code">Code</label><br/> <!-- This was generated by Rails: --> <!-- %= text_field 'rv_wrk_consult_type', 'code' % --> <!-- This is what I replace it with: --> <%= text_field_tag 'code', params[:code], :maxlength => 10 %> </p>

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.

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]