Re: Dynamically creating form fields

Is the association set up in the model correctly?

class Person < ActiveRecord::Base
  has_many :addresses
end

Railscasts - Free Ruby on Rails Screencasts

22

Re: Dynamically creating form fields

class Person < ActiveRecord::Base   
  has_many :person_addresses, :dependent => true
  has_many :addresses, :through => :person_addresses
end

class Address < ActiveRecord::Base
    has_many :person_addresses, :dependent => true
    has_many :persons, :through => :person_addresses   
end

class PersonAddress < ActiveRecord::Base
    belongs_to :person
    belongs_to :address 
end

23

Re: Dynamically creating form fields

Thought this might work but it doesnt sad

    @person.addresses << Address.create(params[:address])
    @person.save

It creates the address and writes it to the db but doesn't add it to the person.

Last edited by mip (2006-11-15 16:47:49)

Re: Dynamically creating form fields

Yeah, those shortcut methods don't work for the has_many :through relationship. You need to create the join model. Try this:

@person.person_addresses.create(:address => Address.create(params[:address])

Railscasts - Free Ruby on Rails Screencasts

25

Re: Dynamically creating form fields

ryanb wrote:

Yeah, those shortcut methods don't work for the has_many :through relationship. You need to create the join model. Try this:

@person.person_addresses.create(:address => Address.create(params[:address])

I'm getting a syntax error on whatever line I put after this.

Edit: missing ')'. It works! smile

Last edited by mip (2006-11-16 05:53:47)

26

Re: Dynamically creating form fields

Which leaves me with only one remaining problem.

The partial which this returns updates a list of the person's addresses. This works very well. The problem is that because I'm updating the list I can't update the div which contains the new address form.

Is there a way I can update two divs? I need to revert the completed form back to the 'Add address' link.

Thanks for the help it's much appreciated.

Re: Dynamically creating form fields

RJS makes it really easy to update multiple divs, so I would do that. It's really not too hard.

First you need to remove mention of the div in the submit_to_remote call:

<%= submit_to_remote 'save', 'Save address', :url => {:action => 'add_address', :id => person.id} %>

Next, remove render statement from the add_address method.

Lastly, create a new file called 'add_address.rjs' and place it in the controller's view directory (with the rhtml files). To do what you are already doing, add this line to the RJS file:

page['address-table'].replace_html :partial => 'address_list', :locals => {:person => @person}

Here you can update as many divs as you want with whatever partials. You can do lots of other cool stuff too, but I won't get into that. For example:

# add to add_address.rjs
page['address-fields'].replace_html :partial => 'address_fields'

Railscasts - Free Ruby on Rails Screencasts

28

Re: Dynamically creating form fields

Many thanks Ryan. I've got something similar working! smile