Trouble passing :id of one model to another model


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 (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")
    <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 width="15%" valign="top" class="tool-title"><%=h tool.priority
    <td width="15%" valign="top" class="tool-title"><%=h tool.owner
    <td width="15%" valign="top" class="tool-title"><%=h tool.eta %></

    <td valign="top"><%= button_to 'Add Task', new_tooltask_path,:id
=> %></td>
    <td valign="top"><%= button_to 'Edit', edit_tool_path(tool) %></

    <td valign="top"><%= button_to 'Destroy', tool, :confirm => 'Are
you sure?', :method => :delete %></td>
<% end %>

     def new
         @tooltask =

         # is this the problem???
         @tool = Tool.find(params[:id])

         respond_to do |format|
           format.html # new.html.erb
           format.xml { render :xml => @tooltask }

What's going on? What am I missing? thanks for any help


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 =>

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:


whereas with a nested route, you could use something like



Works like a dream! thanks Mike, and thanks for the advice!