Editing my question for brevity and updating what I did:
How can I model several addresses for a company and assign one address to a contact and be able to assign them when creating or editing a contact?
I want to use nested attributes in order to be able to add an address while creating a new contact. This address exists as its own model, because I may need the ability to drop out of existing addresses instead of typing from scratch.
I can not make it work. I get an undefined `build 'method for nil: NilClass error
Here is my contact model:
class Contact < ActiveRecord::Base
attr_accessible :first_name, :last_name, :title, :phone, :fax, :email, :company,
:date_entered, :campaign_id, :company_name, :address_id, :address_attributes
belongs_to :company
belongs_to :address
accepts_nested_attributes_for :address
end
Here is my model for the address:
class Address < ActiveRecord::Base
attr_accessible :street1, :street2, :city, :state, :zip
has_many :contacts
end
, , , , . , :
class Company < ActiveRecord::Base
attr_accessible :name, :phone, :addresses
has_many :contacts
has_many :addresses, :through => :contacts
end
_ , , - , :
<% f.fields_for :address, @contact.address do |builder| %>
<p>
<%= builder.label :street1, "Street 1" %> </br>
<%= builder.text_field :street1 %>
<p>
<% end %>
, 1 . , show.html.erb.
- :
:
def new
@contact = Contact.new
@contact.address.build
@contact.date_entered = Date.today
@campaigns = Campaign.find(:all, :order => "name")
if params[:campaign_id].blank?
else
@campaign = Campaign.find(params[:campaign_id])
@contact.campaign_id = @campaign.id
end
if params[:company_id].blank?
else
@company = Company.find(params[:company_id])
@contact.company_name = @company.name
end
end
def create
@contact = Contact.new(params[:contact])
if @contact.save
flash[:notice] = "Successfully created contact."
redirect_to @contact
else
render :action => 'new'
end
end
def edit
@contact = Contact.find(params[:id])
@campaigns = Campaign.find(:all, :order => "name")
end
:
POSTING , ....
Controller # create ( 127.0.0.1 2010-05-12 21:16:17)
[POST] : { "" = > "", "Authenticity_token" = > "D8/gx0zy0Vgg6ghfcbAYL0YtGjYIUC2b1aG + dDKjuSs =", "" = > { "company_name" = > "Allyforce", "title" = > "," campaign_id "= > " 2 "," Address_attributes "= > {" Street1 "= > " ABC "}," fax" = > "," phone" = > "," Last_name" = > "," Date_entered "= > " 2010-05-12 "," email" = > "," first_name "= > " abc "}}
(0.0ms) [0m [0mSELECT * FROM" "WHERE (" "." Name" = "Allyforce" ) LIMIT 1 [0m
(16.0ms) [0m
[0; 1MINSERT INTO " "(" "," zip "," created_at "," street1 "," updated_at "," street2 "," state ") VALUES (NULL, NULL, '2010-05-13 04:16:18 ', NULL,' 2010-05-13 04:16:18 ', NULL, NULL) [0m
Create (0.0ms) [0m
[0mINSERT INTO" "(" "," created_at", "title", "updated_at "," campaign_id "," address_id "," last_name "," phone "," fax "," company_id "," date_entered "," first_name "," email") VALUES (NULL, '2010-05-13 04:16:18', '', '2010-05-13 04:16:18 ', 2, 2,' ',' ',' ', 5, '2010-05-12', 'abc', '') [0m