我的應用程序每次我想查看我的additionals的病人,我得到以下錯誤時間,這有很多的要求,並與它相關聯additionals病人,:導軌 - NoMethodError在控制器
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
編輯 - 更新顯示完整的錯誤信息。
你能顯示整個錯誤消息? – DiodonHystrix
當然,現在已經更新。謝謝參觀 ! –