Pourquoi l'erreur se produit-elle?NoMethodError dans ParticipantsController # new
Il n'y a pas eu de solution appropriée pour mon problème. Je pourrais juste trouver quelques trucs et astuces ici mais maintenant je suis coincé.
Nous avons un système de gestion de cours. Vous pouvez ajouter de nouveaux coureurs, participants et personnes entre autres choses. J'ai dû changer la base de données. Maintenant, il ya aussi une table . Plus tôt toutes les informations sur les personnes respectivement les participants où juste enregistré dans le participants tableau. Maintenant, quand un nouveau participant est ajouté le personnes est impliqué. Nous voulons ajouter un nouveau participant à un cours. J'ai ajusté la nouvelle action dans le contrôleur des participants et j'espérais passer toutes les données comme à l'ancienne. L'ancienne façon travaillait à ajouter un nouveau participant.
Un peu plus tôt le chemin était: cours > nouveau participant forme
Maintenant, il est: cours > recherche d'une personne à l'utiliser sous forme > nouveau formulaire de participation
Je pense (de meilleures façons acceptées) Je viens d'ajuster l'ancien code ?! Voici mon essai.
L'erreur
NoMethodError dans ParticipantsController # nouvelle méthode non définie participants' pour []: Array
se produit.
Voici les anciennes classes:
Modèle cours
class Course < ActiveRecord::Base
has_many :participants
Modèle Participant
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
Lorsque vous regardez ci-dessous l'extrait de la vue du cours, il y a l'ancien et le nouveau chemin vers le formulaire. Notez que la recherche de personne se trouve entre le cours et le nouveau formulaire de participant maintenant.
**old** <%= link_to t(:add), new_course_participant_path(@course) %>
**new** <%= link_to t(:add), course_persons_path(@course, @person)%>
Voici les nouvelles classes
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
Voici mes ajustements du ParticipantsController. Mes pensées sont peut-être naïves parce que j'apprends encore du rubis sur des rails.
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
Merci à l'avance
votre '@course 'à la suite de 'Course.find (: all,: conditions => {: id => @participant})' est un tableau vide, il n'y a aucun moyen d'accéder à 'participants '. –
Merci pour votre réponse. Mais pourquoi id a-t-il travaillé dans l'ancien code? –
Quelle version de Rails utilisez-vous? –