Help associating database tables

I’m trying to write a mechanism to allow a user to input information
about a phone call, including timestamps and various events, among other
things. When a user clicks the New Call link from the main page, the New
Call view is rendered and the ‘new’ method in the controller is invoked
which creates a new row (record) in the ‘calls’ table of the database.
The New Call view has a drop-down menu and a text area for user input,
as well as other Ajax-enabled controls. When the user makes a selection
from the drop-down menu and enters text in the text area and presses the
Submit button, the ‘newEvent’ method creates a new row in the ‘comments’
table of the database.
I’ve tried many things and still cannot get the new row in the ‘events’
table associated with the row in the ‘calls’ table. I need to do this so
I can display to the viewer that event X belongs to call Y.
How can I do this??

In the view:
<% form_for :call, @call, :url => { :action => “newEvent” }, :html =>
{:id => ‘post_event’} do |f| %>

<% @event.each do |event| %> <%= event.eventtype %> <% end %>

<%= text_area :comment, :comment, :class=>“large” %>

<%= submit_tag “Save Event” %>
<% end %>

In the controller:
def newEvent
#carry over the session variable created in
#method ‘new’ like so… session[:call_id] = @call.id
@call = Call.find_by_id(session[:call_id])
@event = Event.find(:all)
@comment = Comment.new(params[:comment])
if @comment.save
flash[:notice] = ‘Successfully added event to dispatch call’
redirect_to :action => ‘list’
end
end

def save_event
session[:post_event] = Call.new(params[:event])
render :text => “Event auto-saved at #{Time.new}
end

Hi Bill

Ryan B. provides a nice solution to multimodel form assiciations in a
series of Railscasts. Here is the link:
#73 Complex Forms Part 1 - RailsCasts. The main differences between his
method and your approach is that he doesn’t use the controller of the
parent method to save the children, he does this in the model… which I
like since it slims down the controller considerably. You might also be
interested to see how he uses RJS/Prototype to render new fields to the
associated model(s) without making asynchronous calls to the server!
Pretty cool id say. Check it out and see if it might not help!

  • Mike

Thank you for the link Mike.
Looking at the screencasts now… I’ve got a good grasp of AJAX, so I
think this is the conceptual watershed I need to cross in order to get
this done.