I have used a combination of answers from 21222015, 17572279, 2663141, and RailsGuide to try and get multiple interdependent models to be created from a form.
In my application Users
are used for authentication and Clinicians
and Patients
are models for the two different types of Users
. Clinicians
and Patients
have almost no attributes in common so creating separate models made sense.
I would like to be able to create a patient
or clinician
and a user
at the same time on a form. Patients
and clinicians
are both connected to their user
by a user_id
integer field.
Right now patients
and clinicians
belongs_to
a user
(patients
also belongs_to
a clinician
and a clinician
has_many
patients
):
class Patient < ActiveRecord::Base
belongs_to :clinician
belongs_to :user
def set_user_id_from_user
patient.user_id = user.id
end
before_validation :set_user_id_from_user
end
A user
has_one
patient
or clinician
:
class User < ActiveRecord::Base
has_secure_password
has_one :patient, :dependent => :destroy
has_one :clinician, :dependent => :destroy
accepts_nested_attributes_for :patient, :allow_destroy => true
accepts_nested_attributes_for :clinician, :allow_destroy => true
validates :email, presence: true
validates_uniqueness_of :email
end
I am trying to create a user
and a patient
on the new.html.erb - patients page using accepts_nested_attributes_for
. I followed the RailsCast Nested Model Form Part 1 as it was recommended as an answer to SO question 10058584. This is my form:
<%= form_for @user do |form| %>
<p>
<div class="form-group">
<%= form.label :email %>
<%= form.text_field :email, class: "form-control", placeholder: "email address" %>
</div>
<div class="form-group">
<%= form.label :password %>
<%= form.password_field :password, class: "form-control", placeholder: "enter password" %>
</div>
</p>
<%= form.fields_for :patient do |builder| %>
<p>
<div class="form-group">
<%= builder.label :first_name %>
<%= builder.text_field :first_name, class: "form-control", placeholder: "First name" %>
</div>
<div class="form-group">
<%= builder.label :last_name %>
<%= builder.text_field :last_name, class: "form-control", placeholder: "Last name" %>
</div>
<div class="form-group">
<%= builder.label :diagnosis %>
<%= builder.text_field :diagnosis, class: "form-control", placeholder: "Diagnosis" %>
</div>
<div class="form-group">
<%= builder.label :gender_id %>
<%= builder.collection_select :gender_id, Gender.all, :id, :gender_type, :prompt => true, class: "form-control" %>
</div>
<div class="form-group">
<%= builder.label :age %>
<%= builder.text_field :age, class: "form-control", placeholder: "Age" %>
</div>
</p>
<% end %>
<%= form.button 'Create Patient', class: "btn btn-u btn-success" %>
<% end %>
In my UsersController
I have:
def new
@user = User.new
end
def create
@user = User.create(user_params)
if @user.save
redirect_to user_path(@user), notice: "User created!"
else
render "new"
end
end
private
def user_params
params.require(:user).permit(:email, :password, patient_attributes: [ :first_name,:last_name,:user_id,:diagnosis,:gender_id,:age,:address, :email, :password, :phone_number, :caregiver_name, :other_symptom, :goals_of_care, :patient_deceased, :patient_archived ])
end
All of this currently gives me a form that only shows the two user
fields: email
and password
. When submitted I get a new user created but no patient is created.
How do I get the patient
fields to appear on the form and have a patient
created that has a user_id
that connects it to the user
created at the same time?
Eventually I need to have it possible to create either a patient
or a clinician
when a user
is created; maybe by making it so that that a patient/clinician
accepts_nested_attributes_for
a user
and have the form for each?
Any advice would be really appreciated, thanks