Update multiple tables with one form


I'm new to this group and new to RoR. Because I want to learn RoR I
defined a project for myself for learning purposes. So I think this
will be my first question of quite a few that will follow.

I try to update multipe tables after sending a form. My form is very
basic by now and has a title and a short description and a long
description. Idea is that there will be multiple description fields
later. So the long desc is in another table. I set up all this foreign
key, belongs_to and has_many stuff and I'm now able to create a new
document in which all data is stored in the right tables. But I'm not
able to update it.

This is how my update method looks right now:

def update
  @document = Document.find(params[:id])
  @text_items = TextItem.find(:all)
  @text_items.each { |ti| @document.text_items << ti }
  if @document.update_attributes(params[:document])
    flash[:notice] = 'Document was successfully updated.'
    redirect_to :action => 'show', :id => @document
    render :action => 'edit'

I did not get an error but the data from my text_items will not be
saved. I read through my books now multiple times and searched the
internet but I did not find a solution.

Can somenone please give me a hint? Thanks.


Hi Stefanm

Stefan Frede wrote:

I try to update multipe tables after sending a form.

The easiest way to handle this is to use form_tag instead of form_for. Then
in your controller you'll individually assign values from the params hash to
the appropriate model.




Thanks. I don't understand it completely what happened in the
screencasts but I think that is a problem I can solve :slight_smile:

@Bill: Thought that this was what I've done but I now think that I
know where my further problems were. Will try to get it running this
way too.