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
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. –
Vielen Dank für Ihre Antwort. Aber warum hat Id im alten Code gearbeitet? –
Welche Rails-Version verwenden Sie? –