svenhecht
2007-Aug-09 22:43 UTC
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 ;) Thanks in Advance --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
svenhecht
2007-Aug-11 18:59 UTC
Re: Error: "Couldn''t find Contact without an ID" -> form_tag parameter confusion
anyone? On Aug 10, 12:43 am, svenhecht <sven.he...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 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 ContactwithoutanID" > #{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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gers32
2007-Aug-17 08:28 UTC
Re: Error: "Couldn''t find Contact without an ID" -> form_tag parameter confusion
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. On Aug 11, 8:59 pm, svenhecht <sven.he...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> anyone? > > On Aug 10, 12:43 am, svenhecht <sven.he...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > 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 ContactwithoutanID" > > #{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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gers32
2007-Aug-17 08:32 UTC
Re: Error: "Couldn''t find Contact without an ID" -> form_tag parameter confusion
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] On Aug 17, 10:28 am, gers32 <cbu.ki...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > On Aug 11, 8:59 pm, svenhecht <sven.he...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > anyone? > > > On Aug 10, 12:43 am, svenhecht <sven.he...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > 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 ContactwithoutanID" > > > #{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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---