Hello, I''m having trouble workign with two models, specifically passing the id of my "Tools" model to New controller of my task model. I am creating a page that keeps track of tools we are building in our group. For each tool, there can be several tasks associated with completing the code/process of the tool. I have a model for tool ("tools") and a model for tasks ("tooltasks"). On the tools view, each tool is listed out in table format. Next to each tool is a button/link called "add task". When the user clicks this button, I would like to bring up the tooltasks#new view with the tool.id (or just tool passed to the form). This is where its not working and i''m stuck. Any help out there? offending code: ---> tools main page = "index.html.erb" <div id="tool-list"> <% for tool in @tools %> <tr valign="top" class="<%=cycle("tool-line-odd","tool-line-even") %>"> <tr> <td width="15%" valign="top" class="tool-title"><%=link_to tool.title, tool %></td> <td width="15%" valign="top" class="tool-title"><%=h tool.description %></td> <td width="15%" valign="top" class="tool-title"><%=h tool.status %></td> <td width="15%" valign="top" class="tool-title"><%=h tool.priority %></td> <td width="15%" valign="top" class="tool-title"><%=h tool.owner %></td> <td width="15%" valign="top" class="tool-title"><%=h tool.eta %></ td> <td valign="top"><%= button_to ''Add Task'', new_tooltask_path,:id => tool.id %></td> <td valign="top"><%= button_to ''Edit'', edit_tool_path(tool) %></ td> <td valign="top"><%= button_to ''Destroy'', tool, :confirm => ''Are you sure?'', :method => :delete %></td> </tr> <% end %> </table> </div> tooltasks_controller.rb def new @tooltask = Tooltask.new # is this the problem???? @tool = Tool.find(params[:id]) respond_to do |format| format.html # new.html.erb format.xml { render :xml => @tooltask } end end What''s going on? What am I missing? thanks for any help Dave --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 2/29/08, loominator1970 <loominator1970-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello, > > I''m having trouble workign with two models, specifically passing the > id of my "Tools" model to New controller of my task model. > > I am creating a page that keeps track of tools we are building in our > group. For each tool, there can be several tasks associated with > completing the code/process of the tool. I have a model for tool > ("tools") and a model for tasks ("tooltasks"). On the tools view, > each tool is listed out in table format. Next to each tool is a > button/link called "add task". When the user clicks this button, I > would like to bring up the tooltasks#new view with the tool.id (or > just tool passed to the form). This is where its not working and i''m > stuck. Any help out there? > > offending code: > > ---> tools main page = "index.html.erb" > <div id="tool-list"> > <% for tool in @tools %> > <tr valign="top" class="<%=cycle("tool-line-odd","tool-line-even") > %>"> > <tr> > <td width="15%" valign="top" class="tool-title"><%=link_to > tool.title, tool %></td> > <td width="15%" valign="top" class="tool-title"><%=h > tool.description %></td> > <td width="15%" valign="top" class="tool-title"><%=h tool.status > %></td> > <td width="15%" valign="top" class="tool-title"><%=h tool.priority > %></td> > <td width="15%" valign="top" class="tool-title"><%=h tool.owner > %></td> > <td width="15%" valign="top" class="tool-title"><%=h tool.eta %></ > td> > <td valign="top"><%= button_to ''Add Task'', new_tooltask_path,:id > => tool.id %></td> > <td valign="top"><%= button_to ''Edit'', edit_tool_path(tool) %></ > td> > <td valign="top"><%= button_to ''Destroy'', tool, :confirm => ''Are > you sure?'', :method => :delete %></td> > </tr> > <% end %> > </table> > </div> > > > tooltasks_controller.rb > def new > @tooltask = Tooltask.new > > # is this the problem???? > @tool = Tool.find(params[:id]) > > respond_to do |format| > format.html # new.html.erb > format.xml { render :xml => @tooltask } > end > endwhat''s the error that''s produced? What''s the backtrace of the error? What does the error message indicate is the offending line? I assume you''re getting a ActiveRecord::RecordNotFound exception. What are the params being passed to your TooltasksController#new method? I''m guessing that params[:id] is actually nil.. Try using: new_tooltask_path(:tool_id => tool.id) and in your controller: @tool = Tool.find_by_id(params[:tool_id]) also remember to never call Class.find() without enclosing it within a begin/rescue block, otherwise you''ll raise an exception if no record is found. Or use find_by_id and check to make sure that it actually returns a record. You could also look into nested routes if you want to make your urls look prettier, since the above will generate something like: tooltask/new?tool_id=1234 whereas with a nested route, you could use something like tools/1234/tasks/new Mike --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
loominator1970
2008-Feb-29 22:04 UTC
Re: Trouble passing :id of one model to another model
Works like a dream! thanks Mike, and thanks for the advice! Dave On Feb 29, 2:31 pm, "Mike Garey" <random...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 2/29/08, loominator1970 <loominator1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hello, > > > I''m having trouble workign with two models, specifically passing the > > id of my "Tools" model to New controller of my task model. > > > I am creating a page that keeps track of tools we are building in our > > group. For each tool, there can be several tasks associated with > > completing the code/process of the tool. I have a model for tool > > ("tools") and a model for tasks ("tooltasks"). On the tools view, > > each tool is listed out in table format. Next to each tool is a > > button/link called "add task". When the user clicks this button, I > > would like to bring up the tooltasks#new view with the tool.id (or > > just tool passed to the form). This is where its not working and i''m > > stuck. Any help out there? > > > offending code: > > > ---> tools main page = "index.html.erb" > > <div id="tool-list"> > > <% for tool in @tools %> > > <tr valign="top" class="<%=cycle("tool-line-odd","tool-line-even") > > %>"> > > <tr> > > <td width="15%" valign="top" class="tool-title"><%=link_to > > tool.title, tool %></td> > > <td width="15%" valign="top" class="tool-title"><%=h > > tool.description %></td> > > <td width="15%" valign="top" class="tool-title"><%=h tool.status > > %></td> > > <td width="15%" valign="top" class="tool-title"><%=h tool.priority > > %></td> > > <td width="15%" valign="top" class="tool-title"><%=h tool.owner > > %></td> > > <td width="15%" valign="top" class="tool-title"><%=h tool.eta %></ > > td> > > <td valign="top"><%= button_to ''Add Task'', new_tooltask_path,:id > > => tool.id %></td> > > <td valign="top"><%= button_to ''Edit'', edit_tool_path(tool) %></ > > td> > > <td valign="top"><%= button_to ''Destroy'', tool, :confirm => ''Are > > you sure?'', :method => :delete %></td> > > </tr> > > <% end %> > > </table> > > </div> > > > tooltasks_controller.rb > > def new > > @tooltask = Tooltask.new > > > # is this the problem???? > > @tool = Tool.find(params[:id]) > > > respond_to do |format| > > format.html # new.html.erb > > format.xml { render :xml => @tooltask } > > end > > end > > what''s the error that''s produced? What''s the backtrace of the error? > What does the error message indicate is the offending line? I assume > you''re getting a ActiveRecord::RecordNotFound exception. What are the > params being passed to your TooltasksController#new method? I''m > guessing that params[:id] is actually nil.. > > Try using: > > new_tooltask_path(:tool_id => tool.id) > > and in your controller: > > @tool = Tool.find_by_id(params[:tool_id]) > > also remember to never call Class.find() without enclosing it within a > begin/rescue block, otherwise you''ll raise an exception if no record > is found. Or use find_by_id and check to make sure that it actually > returns a record. > > You could also look into nested routes if you want to make your urls > look prettier, since the above will generate something like: > > tooltask/new?tool_id=1234 > > whereas with a nested route, you could use something like > > tools/1234/tasks/new > > Mike--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---