'Rails add search result to registration#new form
In my Rails 5 app I've got registration#new form where Admin/Doctor search for a Patient for that registration. From the result, after pushed Add Patient button, the selected patient should be transferred to the registration#new form (searched results are on the same page) without reloading the page (so that you do not lose any fields that you filled out earlier) with notice 'Patient Added'. Here is my code:
registrants_controller.rb
class RegistrantsController < ApplicationController
def new
@patient = Registrant.find(session[:patient_id_to_add_caregiver]) if session[:patient_id_to_add_caregiver]
@registrant = Registrant.new
respond_to do |format|
format.html
format.js { render partial: 'patient_search_result' }
end
end
def create
@registrant = Registrant.new(parse_post_params)
@patient = Registrant.find(session[:patient_id_to_add_caregiver]) if session[:patient_id_to_add_caregiver]
if @registrant.save
CaregiverPatient.create(patient: @patient, caregiver: @registrant) if @patient.present?
redirect_to registrant_path(@registrant), notice: 'Registrant Added'
else
render :new
end
end
registrants/_new_form.html.erb
<%= form_for @registrant do |f| %>
<%= f.fields_for :registration do |registration| %>
<% if @patient %>
Patient Added: <%= @patient.full_name %>
<% else %>
<%= required_text_field_group registration, @registrant, :registrant_id, "Patient Added", {}, { disabled: true } %>
<% end %>
<% end %>
# some other logic (...)
# patient search result
<% @registrants do |registrant| %>
<%= link_to 'Add Patient', new_registrant_path %>
<% end %>
So how to add a Patient without reloading the page to be visible in Patient Added: <%= @patient.full_name %> if my format.js is occupied by search results that must also appear without reloading the page?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
