2017-04-09 23 views
1

Meine Anwendung hat einen Patient, die viele Anfragen und additionals zugeordnet hat, jedes Mal wenn ich versuche, mein additionals für einen Patient sehe ich die folgende Fehlermeldung erhalten:Rails - NoMethodError in Controller-

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 

BEARBEITEN - aktualisiert, um vollständige Fehlermeldung anzuzeigen.

+0

Könnten Sie zeigen die ganze Fehlermeldung? – DiodonHystrix

+0

Sicher, jetzt aktualisiert. Danke für einen Blick! –

Antwort

1

Vergewissern Sie sich immer, dass Sie die gesamte Fehlermeldung gelesen haben.

Es sieht aus wie Sie einen Rechtschreibfehler additonals (aus dem „Hast du Mean“) haben

Höchstwahrscheinlich werden Sie Dinkel Ihre has_many :additionals Beziehung falsch.

+0

Das war der genaue Fehler, mein böses tut mir leid. Ich habe es zu lange angeguckt und es nicht bemerkt! Danke noch einmal ! –

1

Im Patient Modell Sie haben:

has_many :additonals 

Es sollte:

has_many :additionals 
+0

Danke Kumpel, hätte das wirklich sehen sollen. Ich habe es den größten Teil des Tages angeguckt und es verpasst. Prost –

+0

Sehen Sie sich beim nächsten Mal eine Fehlermeldung genauer an! :) – DiodonHystrix

Verwandte Themen