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”
<% for tool in @tools %>
">
<%=link_to
tool.title, tool %> |
<%=h
tool.description %> |
<%=h tool.status
%> |
<%=h tool.priority
%> |
<%=h tool.owner
%> |
<%=h tool.eta %>
|
<%= button_to 'Add Task', new_tooltask_path,:id
=> tool.id %> |
<%= button_to 'Edit', edit_tool_path(tool) %>
|
<%= button_to 'Destroy', tool, :confirm => 'Are
you sure?', :method => :delete %> |
<% end %>
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
On 2/29/08, loominator1970 [email protected] wrote:
each tool is listed out in table format. Next to each tool is a
<tr valign="top" class="<%=cycle("tool-line-odd","tool-line-even")
<%=h tool.owner
<% end %>
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
Works like a dream! thanks Mike, and thanks for the advice!
Dave