2017-04-09 165 views
1

我的應用程序每次我想查看我的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 

編輯 - 更新顯示完整的錯誤信息。

+0

你能顯示整個錯誤消息? – DiodonHystrix

+0

當然,現在已經更新。謝謝參觀 ! –

回答

1

務必確保您閱讀完整的錯誤信息。

它看起來像你有拼寫錯誤additonals(從「你的意思」)

最有可能你拼寫你的has_many :additionals關係是錯誤的。

+0

這是確切的錯誤,我很抱歉。盯着它太久了,沒有注意到!再次感謝 ! –

1

Patient模型,你有:

has_many :additonals 

它應該是:

has_many :additionals 
+0

感謝隊友,真的應該看到了。我一直盯着這一天的最好的一部分,錯過了它。歡呼 –

+0

下次更好地看看錯誤信息! :) – DiodonHystrix