我的应用程序有一个患者,其中有许多请求和附加功能,每当我尝试查看患者的附加信息时,我会收到以下错误:
NoMethodError in AdditionalsController#index
undefined method `additionals' for #<Patient:0x007fb3e7c1aaf0> Did you mean? additonals additonals=
Extracted source (around line #8):
6 @patient = Patient.find(params[:patient_id])
7 # Access all additionals for that patient
8 @additionals = @patient.additionals
9 end
10
patient.rb
class Patient < ApplicationRecord
has_many :requests
has_many :additonals
def self.search(search_for)
Patient.where("last_name = ?", search_for)
end
end
additional.rb
class Additional < ApplicationRecord
belongs_to :patient
end
additional_controller.rb
class AdditionalsController < ApplicationController
def index
@patient = Patient.find(params[:patient_id])
# Access all additionals for that patient
@additionals = @patient.additionals
end
# GET /patients/1/additionals/2
def show
@patient = Patient.find(params[:patient_id])
# For URL like /patients/1/additionals/2
# Find an additional in patients 1 that has id=2
@additional = @patient.additionals.find(params[:id])
end
# GET /patients/1/additionals/new
def new
@patient = Patient.find(params[:patient_id])
# Associate an additional object with patient 1
@additional = @patient.additionals.build
end
# POST /patients/1/additionals
def create
@patient = Patient.find(params[:patient_id])
# For URL like /patients/1/additionals
# Populate an additional associate with patient 1 with form data
# Patient will be associated with the additional
# @additional = @patient.additionals.build(params.require(:additional).permit!)
@additional = @patient.additionals.build(params.require(:additional).permit(:date, :cost, :note))
if @additional.save
# Save the additional successfully
redirect_to patient_additional_url(@patient, @additional)
else
render :action => "new"
end
end
# GET /patients/1/additionals/2/edit
def edit
@patient = Patient.find(params[:patient_id])
# For URL like /patients/1/additionals/2/edit
# Get additional id=2 for patient 1
@additional = @patient.additionals.find(params[:id])
end
# PUT /patients/1/additionals/2
def update
@patient = Patient.find(params[:patient_id])
@additional = Additional.find(params[:id])
if @additional.update_attributes(params.require(:additional).permit(:date, :cost, :note))
# Save the additional successfully
redirect_to patient_additional_url(@patient, @additional)
else
render :action => "edit"
end
end
# DELETE /patients/1/additionals/2
def destroy
@patient = Patient.find(params[:patient_id])
@additional = Additional.find(params[:id])
@additional.destroy
respond_to do |format|
format.html { redirect_to patient_additionals_path(@patient) }
format.xml { head :ok }
end
end
end
编辑 - 已更新以显示完整的错误消息。
答案 0 :(得分:1)
始终确保您阅读完整的错误消息。
看起来您的拼写错误additonals
(来自&#34;您的意思是&#34;)
您很可能拼错了has_many :additionals
关系。
答案 1 :(得分:1)
在您拥有的has_many :additonals
模型中:
has_many :additionals
应该是:
silent! unmap <C-E>