Hi! I''m creating a simple signup form where people can sign up for a camp. They should be able to add a contact person and also sign up other people (such as family members). Two models: Application has_many :participants accepts_nested_attributes_for :participants Participant belongs_to :application I have some trouble with the contact person, which is also a participant. I guess I should add something like this: Application has_many :participants belongs_to :contact_person, :class_name => "Participant", :foreign_key => "contact_person_id" accepts_nested_attributes_for :participants, :contact_person Ok, so then I try to set up my nested form. In my ApplicationsController#new method I do this: def new @application = Application.new 2.times { @application.participants.build } 1.times { @application.contact_person.build } end But that gives me an error: "undefined method `build'' for nil:NilClass". The error comes from: "@application.contact_person.build", so contact_person is nil. So, any ideas what I have done wrong? It was a while ago I fiddled with nested forms like this... Thanks! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/RMu_z5Xl3zwJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Peter Vandenabeele
2012-Mar-31 19:20 UTC
Re: How to set up these associations and nested form
On Sat, Mar 31, 2012 at 7:59 PM, Linus Pettersson <linus.pettersson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > > I''m creating a simple signup form where people can sign up for a camp. They > should be able to add a contact person and also sign up other people (such > as family members). > > Two models: > Application > has_many :participants > accepts_nested_attributes_for :participants > > Participant > belongs_to :application > > I have some trouble with the contact person, which is also a participant. I > guess I should add something like this: > > Application > has_many :participants > belongs_to :contact_person, :class_name => "Participant", :foreign_key => > "contact_person_id" > accepts_nested_attributes_for :participants, :contact_person > > Ok, so then I try to set up my nested form. In my ApplicationsController#new > method I do this: > > def new > @application = Application.new > 2.times { @application.participants.build } > 1.times { @application.contact_person.build } > end > > But that gives me an error: "undefined method `build'' for nil:NilClass".The > error comes from: "@application.contact_person.build", so contact_person is > nil.The correct wording is (somewhat confusingly) @application.build_contact_person Check these tables for the exact names of the singular and plural associations: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html HTH, Peter -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.