我有兩個模型:一個用於患者,另一個用於引薦請求。我創建了兩種表單 - 一種允許用戶創建新病人,另一種允許用戶創建轉診請求。患者可以有很多轉診請求,並且轉診請求將屬於創建它的用戶以及爲其創建的患者。Rails:將變量從一個控制器傳遞到另一個控制器,用於兩種形式
我很難弄清楚如何將這些表單連接在一起,以創建病人和用戶爲他們創建的轉診請求之間的關聯。我認爲我必須在新內容中添加一些內容或創建引薦請求控制器的操作,但我無法獲得任何我嘗試過的工作。
任何幫助讚賞菜鳥!
患者控制器:
class PatientsController < ApplicationController
def new
@patient = current_user.patients.build
end
def create
@patient = current_user.patients.build(patient_params)
if @patient.save
flash[:success] = "Patient Created!"
redirect_to '/referral_requests/new'
else
Rails.logger.info(@patient.errors.inspect)
render 'patients/new'
end
end
private
def patient_params
params.require(:patient).permit(:age, :user_id, insurance_ids: [], gender_ids: [], concern_ids: [], race_ids: [])
end
end
轉診請求控制器:
class ReferralRequestsController < ApplicationController
before_action :logged_in_user, only: [:show, :index, :new, :create, :edit, :update, :destroy]
def index
if logged_in?
@referral_request = current_user.referral_requests.build
@feed_items = current_user.feed.paginate(page: params[:page])
else
@feed_items =[]
end
end
def create
@referral_request = current_user.referral_requests.build(referral_request_params)
if @referral_request.save
flash[:success] = "Referral Request Created!"
render 'referral_requests/index'
else
Rails.logger.info(@referral_request.errors.inspect)
@feed_items = []
render 'static_pages/home'
end
end
def destroy
end
def new
@referral_request = current_user.referral_requests.build if logged_in?
end
private
def referral_request_params
params.require(:referral_request).permit(:content, concern_ids: [],
insurance_ids: [], race_ids: [], language_ids: [], gender_ids: [])
end
end