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@googlegroups.com.
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.

Search Discussions

  • Peter Vandenabeele at Mar 31, 2012 at 7:20 pm

    On Sat, Mar 31, 2012 at 7:59 PM, Linus Pettersson 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@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 31, '12 at 7:00p
activeMar 31, '12 at 7:20p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase