How to create a nested signup form?

I have two models each with their own resources: Organizations and Members. An organization has multiple members and a member is only a member to one organization.

How can I create a simple nested form that signs up a new organization as well as one member for that organization?

My current attempt is below. On submitting the form, this however generates the error message

unknown attribute ‘org_name’ for Member


, which I don’t get especially since org_name is a column within the Organization model and not the Member model.

Organization model

has_many :members, dependent: :destroy
accepts_nested_attributes_for :members, :reject_if => :all_blank, :allow_destroy => true

**Member model**

belongs_to :organization

Organizations controller

def new @organization = @member = end

def create @organization = @member = if && @member.send_activation_email #Will Rails be able to get to this method? The send_activation_email method exists in multiple models and Rails should here use the version from the Member model. flash[:success] = “Please check your email to activate your account.” redirect_to root_url else render ‘new’ end end

def organizationnew_params params.require(:organization).permit(:org_name, :phone, member_attributes: [:email, :username, :admin, :password, :password_confirmation ]) end

Organizations new view

<%= form_for(@organization) do |f| %> <%= f.text_field :org_name, %> <%= f.text_field :phone %>

<%= f.fields_for :member do |p| %> <%= p.text_field :username %> <%= p.email_field :email %> <%= p.password_field :password %> <%= p.password_field :password_confirmation %> <%= hidden_field_tag :admin, true %> <% end %>

<%= f.submit “Sign up”, class: “formbutton btn btn-default” %> <% end %>

What am I doing wrong? How can I create a simple nested form?

An update:

In the new view it turned out I needed <%= f.fields_for :members do |p| %>


So plural “members”. Similar, in the params in the controller I changed member_attributes to members_attributes, again the plural form.

Also, in def create in the organizations controller, I removed the line: @member =


Having done all this, I now get a flash error message on submission of the form: The form contains 1 error:

  • Members organization can’t be blank


The server log:

Processing by OrganizationsController#create as HTML
 Parameters: {"utf8"=>"✓", "organization"=>{"org_name"=>"sadfsdaf", "phone"=>"sdfds", "members_attributes"=>{"0"=>{"username"=>
   "fsfdsfsad", "email"=>"", "fullname"=>"sdfds", "password"=>"[FILTERED]",
   "password_confirmation"=>"[FILTERED]"}}}, "admin"=>"true", "commit"=>"Sign up"}
    (0.5ms) begin transaction
 Member Exists (0.4ms) SELECT 1 AS one FROM "members" WHERE LOWER("members"."email") = LOWER('') LIMIT 1
 Member Exists (0.2ms) SELECT 1 AS one FROM "members" WHERE LOWER("members"."username") = LOWER('fsfdsfsad') LIMIT 1
 Organization Exists (0.3ms) SELECT 1 AS one FROM "organizations" WHERE LOWER("organizations"."org_name") = LOWER('sadfsdaf') LIMIT 1
 Organization Exists (0.2ms) SELECT 1 AS one FROM "organizations" WHERE LOWER("organizations"."phone") = LOWER('sdfds') LIMIT 1
  (0.2ms) rollback transaction
 Rendered shared/_error_messages.html.erb (1.9ms)

The **member model** includes:

belongs_to :organization
validates :organization_id, presence: true  # There are other validations as well, but I don't think they matter for this question.
def send_activation_email


Next, if I remove the **validates :organization_id** line from the member model, the form works fine. It saves the organization, the member, as well as the organization_if for the member. However, I of course need the model validation as protection.
Any idea how I should move forward?