Ilan Berci wrote:
> James West wrote:
>> Further testing shows that if I use a hidden contact_id field in the 
>> address form the contact_id gets passed to the params hash as I would 
>> expect but I still get the
>> 
> 
> 
> You either have to change:
> 
>      has_one :administrator,
>              :dependent => :destroy
> 
> 
> 
> to:
>      has_many :administrators,
>              :dependent => :destroy
> 
> 
> or remove the administrator_addresses association and do this..
> 
> def administrator_addresses
>    administrator.addresses
> end
> 
> your through relation should go through a 'has_many' and not a 
> 'has_one'..
> 
> hth
> 
> ilan

Thank you for your reply,

I have changed my association because what you say makes total sense 
however I'm still getting the same error.
I'm starting to think that the accepts_nested_attributes is broken.

I now have in my models.
class User < ActiveRecord::Base

  named_scope :find_administrators, :joins => :administrator

  has_one :administrator,
          :dependent => :destroy

  has_many :user_roles,
           :dependent => :destroy

  has_many :addresses,
           :through => :user_roles,
           :source => :addresses,
           :foreign_key => :contact_id,
           :dependent => :destroy

  accepts_nested_attributes_for :addresses, :allow_destroy => true

...
end

class UserRole < ActiveRecord::Base

  has_many :addresses, :foreign_key => :contact_id,
           :dependent => :destroy

  has_many :emails, :foreign_key => :contact_id,
          :dependent => :destroy

  belongs_to :user

  accepts_nested_attributes_for :addresses, :allow_destroy => true

end

class Address < ActiveRecord::Base

  belongs_to :user_role, :foreign_key => :contact_id

  validates_presence_of :contact_id

  validates_presence_of :house

end

class Administrator < UserRole
  #To do - Move this to a boolean flag on the user_role table.
  has_one :site_contact, :foreign_key => :user_role_id
end

Relevant controller methods
class Admin::UsersController < Admin::HomeController

  # GET /users/1/edit
  def edit
    @user = User.find(params[:id])
  end

  # PUT /users/1
  # PUT /users/1.xml
  def update
    logger.debug("#### update params = #{params}")
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        flash[:notice] = "#{show_user_type} was successfully updated."
        format.html { redirect_to([:admin, @user]) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user.errors, :status => 
:unprocessable_entity }
      end
    end
  end

Helper methods
  #
  # Support methods for nested forms
  #
  def link_to_toggle_child_list(div_to_toggle, link_text)
    link_to_function link_text do |page|
      page << "jQuery('#{div_to_toggle}).toggle('slow')"
    end
  end

  def add_address_link(form_builder)
    link_to_function 'New address' do |page|
      form_builder.fields_for :addresses,
                              Address.new,
                              :child_index => 'NEW_RECORD' do |f|
        html = render(:partial => 'address_form', :locals => { :f => f 
})
        page << 
"jQuery('#maintenance_form').replaceWith('#{escape_javascript(html)}'.replace(/NEW_RECORD/g,
 
new Date().getTime()))"
      end
    end
  end
end

Views
_form.html.erb

<%= f.error_messages -%>
<div class="admin-form">
  <fieldset>
    <legend><%= show_user_type %></legend>
    <p>
      <%= f.label :public_name -%>:
      <%= f.text_field :public_name -%>
    </p>
    <p>
      <%= f.label :name -%>:
      <%= f.text_field :name -%>
    </p>
    <p>
      <%= f.label :password, 'Password' -%>:
      <%= f.password_field :password, :size => 40-%>
    </p>
    <p>
      <%= f.label :password_confirmation, "confirm" -%>:
      <%= f.password_field :password_confirmation, :size => 40-%>
    </p>
  </fieldset>
</div>
<%= link_to_function "Show/Hide addresses", {:id => "toggle_children"}%>

div id ="address_list">
  <%= add_address_link(f) %>
  <%= show_address_list(@user) %>
</div>
<div id ="maintenance_form">
</div>

_address_form.html.erb

<%= f.hidden_field :contact_id%>
<fieldset>
  <legend>Details...</legend>
    <p>
      House name/number<br />
      <%= f.text_field :house %>
    </p>
    <p>
      Street<br />
      <%= f.text_field :street %>
    </p>
    <p>
      Town<br />
      <%= f.text_field :town %>
    </p>
    <p>
      city<br />
      <%= f.text_field :city %>
    </p>
    <p>
      Postcode<br />
      <%= f.text_field :postcode %>
    </p>
  <%= link_to_function 'Done' %>
</fieldset>
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to