我有一個控制器(患者)將patient_id傳遞給另一個控制器(referral_requests)以用於構建引薦請求記錄。嘗試創建引薦請求時,我遇到以下錯誤。我究竟做錯了什麼?我的referral_requests表包含user_id的整數列。Rails:ActiveModel創建新記錄時出錯
我有多個用戶類型定義與枚舉。工作人員用戶創建患者和轉診請求。
感謝您的幫助!
錯誤:
(0.1ms) rollback transaction
#<ActiveModel::Errors:0x007fe696696fc0 @base=#<ReferralRequest id: nil, content: nil, user_id: nil, created_at: nil, updated_at: nil, patient_id: 4, call_option_1: nil, call_option_2: nil, call_option_3: nil, call_option_1_status: nil, call_option_2_status: nil, call_option_3_status: nil, status: "created">, @messages={:user_id=>["can't be blank"]}, @details={:user_id=>[{:error=>:blank}]}>
患者控制器創建行動:
def create
@patient = current_user.patients.build(patient_params)
if @patient.save
flash[:success] = "Patient Created!"
redirect_to new_referral_request_path(patient_id: @patient.id)
else
Rails.logger.info(@patient.errors.inspect)
render 'patients/new'
end
referral_requests控制器新創建行動,而params:
def new
@patient = Patient.find(params[:patient_id])
@referral_request = current_user.referral_requests.build(patient: @patient) if signed_in?
end
def create
@referral_request = current_user.referral_requests.build(referral_request_params)
if @referral_request.save
flash[:success] = "Referral Request Created!"
redirect_to new_dispatch_path(referral_request_id: @referral_request.id)
else
Rails.logger.info(@referral_request.errors.inspect)
@feed_items = []
render 'static_pages/home'
end
end
private
def referral_request_params
params.require(:referral_request).permit(:content, :user_id, :patient_id, concern_ids: [], insurance_ids: [], race_ids: [], language_ids: [], gender_ids: [])
end
這裏是模型中的關係:
require 'active_support/concern'
module StaffUser
extend ActiveSupport::Concern
included do
has_many :referral_requests, foreign_key: "user_id"
class ReferralRequest < ApplicationRecord
belongs_to :user, -> { where role: :staff }, class_name: 'User', foreign_key: 'user_id'
謝謝Max - 與淺:真的,你打算使用referral_requests而不是推介?我只是想確保沒有一些別名正在進行,我不理解。 – mike9182
是的,它應該是'resources :: referral_requests,shallow:true'。 – max
這是非常有用的 - 爲什麼patient_id不包含在params中? – mike9182