I'm trying to build an app where when a user signs up, the user along with an organization and a membership to that organization are created. However, I continue to run into an issue where the user is created, but neither the organization nor the membership are.
I have tried many things, but mostly keep getting this same error:
Unpermitted parameter: :organization. Context: { controller: Users::RegistrationsController, action: create, request: #<ActionDispatch::Request:0x0000000117db8248>, params: {"authenticity_token"=>"[FILTERED]", "user"=>{"first_name"=>"John", "last_name"=>"Doe", "email"=>"john@test.com", "position"=>"CEO", "organization"=>{"name"=>"JohnDoeCo"}, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up", "controller"=>"users/registrations", "action"=>"create"} }
My models are as such:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_one :membership
has_one :organization, through: :membership
accepts_nested_attributes_for :membership, :organization
end
class Organization < ApplicationRecord
has_many :memberships
has_many :users, through: :memberships
validates :name, presence: true
end
class Membership < ApplicationRecord
belongs_to :organization
belongs_to :user
end
My Registrations Controller is:
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
# GET /resource/sign_up
def new
super
resource.build_membership
resource.build_organization
end
# POST /resource
def create
super
end
# GET /resource/edit
def edit
super
end
# PUT /resource
def update
super
end
# DELETE /resource
def destroy
super
end
protected
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up) do |u|
u.permit(
:first_name,
:last_name,
:position,
:email,
:password,
:password_confirmation,
organization_attributes: [:name],
membership_attributes: [:organization_id, :user_id]
)
end
end
end
And my view for registrations/new looks like:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "users/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :position %><br />
<%= f.text_field :position %>
</div>
<%= f.fields_for :organization do |fm| %>
<%= fm.label :name %><br />
<%= fm.text_field :name %>
<% end %>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "users/shared/links" %>