2017-10-12 20 views
0

Warum tritt der Fehler auf?NoMethodError in ParticipantsController # Neu

Dort war keine adquate/ähnliche Lösung für mein Problem. Ich könnte hier nur ein paar Tipps und Tricks finden, aber jetzt stecke ich fest.

Wir haben ein Kurs-Management-System. Sie können unter anderem neue Courses, Teilnehmer und Personen hinzufügen. Ich musste die Datenbank ändern. Jetzt gibt es auch einen Personen Tisch. Zuvor waren alle Informationen über Personen bzw. Teilnehmer nur in der Teilnehmer Tabelle gespeichert. Wenn jetzt ein neuer Teilnehmer hinzugefügt wird, ist die Tabelle Personen beteiligt. Wir möchten einen neuen Teilnehmer eines Kurses hinzufügen. Ich korrigierte die neue Aktion in der Teilnehmer-Controller und ich hoffte, alle Daten wie auf die alte Art und Weise zu übergeben. Der alte Weg arbeitete, um einen neuen Teilnehmer hinzuzufügen.

Früher war der Weg: Kurs > neue Teilnehmer Form

Nun ist es: Kurs > Suche nach einer Person, die es in der Form zu verwenden > neue Teilnehmer Form

Ich denke, (Bessere Wege akzeptiert) Ich passe einfach den alten Code an ?! Unten ist mein Versuch.

Der Fehler

NoMethodError in ParticipantsController # new nicht definierte Methode `Teilnehmer für []: Array

auftritt.

Hier sind die alten Klassen:

Modell Course

class Course < ActiveRecord::Base 
    has_many :participants 

Modell Teilnehmer

class Participant < ActiveRecord::Base 
    belongs_to :course 
    belongs_to :organization 
    belongs_to :function 

ParticipantsController

class ParticipantsController < ApplicationController 

.... 
def new 
    @course = Course.find(params[:course_id]) 
    @participant = @course.participants.build 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @participant } 
    end 
    end 

def create 
    @course = Course.find(params[:course_id]) 
    @participant = @course.participants.new(params[:participant]) 
    @course.updated_by = current_user.cn 
    @course.send(:create_version) 
    @course.tag_version(t(:participant_added)) 
    @course.save! 

    respond_to do |format| 
     if @participant.save 
     format.html { redirect_to course_path(@participant.course), notice: 'Participant was successfully created.' } 
     format.json { render json: @participant, status: :created, location: @participant } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @participant.errors, status:  :unprocessable_entity } 
     end 
    end 
    end 

Wenn Sie unten im Kursansicht-Snippet sehen, gibt es den alten und neuen Pfad zum Formular. Beachten Sie, dass die Personensuche jetzt zwischen dem Kurs und dem neuen Teilnehmerformular liegt.

**old** <%= link_to t(:add), new_course_participant_path(@course) %> 

**new** <%= link_to t(:add), course_persons_path(@course, @person)%> 

Hier sind die neuen Klassen

class Participant < ActiveRecord::Base 
    belongs_to :course 
    belongs_to :function 
    belongs_to :person 

class Person < ActiveRecord::Base 
    has_many :participants 
    has_many :courses, through: :participants 

Hier meine Anpassungen im ParticipantsController sind. Meine Gedanken sind vielleicht naiv, weil ich immer noch Ruby auf Schienen lerne.

class ParticipantsController < ApplicationController 
    def new 
     @person = Person.find(params[:person_id]) 
     @participant = Participant.find_by_person_id(params[:person_id]) 
     @course= Course.find(:all, :conditions => {:id => @participant}) 
     @participant = @course.participants.build 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @participant } 
    end 
    end 


def create 
    @course= Course.find(params[:course_id]) 
    @participant = @course.participants.new(params[:participant]) 
    @course.updated_by = current_user.cn 
    @course.send(:create_version) 
    @course.tag_version(t(:participant_added)) 
    @course.save! 

    respond_to do |format| 
     if @participant.save 
     format.html { redirect_to course_path(@participant.course), notice:  'Participant was successfully created.' } 
     format.json { render json: @participant, status: :created, location: @participant } 

     else 
     format.html { render action: "new" } 
     format.json { render json: @participant.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

Vielen Dank im Voraus

+0

gibt '@course 'als Folge von 'Course.find (: all,: conditions => {: id => @participant})' ist ein leeres Array, es gibt keine Möglichkeit, von dort auf 'Teilnehmer' zuzugreifen. –

+0

Vielen Dank für Ihre Antwort. Aber warum hat Id im alten Code gearbeitet? –

+0

Welche Rails-Version verwenden Sie? –

Antwort

0

Rewrite:

@person = Person.find(params[:person_id]) 
@participant = Participant.find_by_person_id(params[:person_id]) 
@course= Course.find(:all, :conditions => {:id => @participant}) 
@participant = @course.participants.build 

An:

@person = Person.find(params[:person_id]) 
@course = Participant.find_by_person_id(params[:person_id]).course 
@participant = @course.participants.build 

Uhr für den Fall heraus für die Ausnahme Participant.find_by_person_id(params[:person_id]) eine Ihre nil

+0

Danke! Es funktioniert jetzt. –

+0

@MarcelB BTW, brauchst du wirklich dieses '@ person' Objekt? Benutzt du es irgendwo? Ich denke, keine Notwendigkeit, es zu laden – AntonTkachov

+0

Ich benutze die Person in der Form. Wie eine Autofill-Funktion. Wenn ein berechtigter Benutzer einen neuen Teilnehmer hinzufügen möchte, kann der Benutzer nach bereits vorhandenen Personen suchen. Wenn eine Person bereits als Teilnehmer im System in der Form vorhanden ist, lade ich die gesuchte Person. Die Felder in der Form sind "readonly: true". <% = F.text_field: Vorname, Wert: @ person.prename, readonly: true, style: 'Hintergrundfarbe: #BDBDBD', Größe: 35%> ' –