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 = Organization.new
@member = @organization.members.build
end
def create
@organization = Organization.new(organizationnew_params)
@member = @organization.members.build(organizationnew_params)
if @organization.save && @member.save
@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?
<%= f.fields_for :members do |p| %>
@member = @organization.members.build(organizationnew_params)
The form contains 1 error:
* Members organization can't be blank
The member model includes:
Processing by OrganizationsController#create as HTML
Parameters: {"utf8"=>"✓", "organization"=>{"org_name"=>"sadfsdaf", "phone"=>"sdfds", "members_attributes"=>{"0"=>{"username"=>
"fsfdsfsad", "email"=>"sfd...@sfsdf.com", "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('sfd...@sfsdf.com') 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)
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
MemberMailer.account_activation(self).deliver_now
end