gelen bir CSV dosya yükleme Eşleştirme tablodaki kayıtları oluşturmak çalışıyorum. Verilen dosya bu biçimde olacaktırİthalat ve oluşturma Kayıtlar CSV
supervisor,student,project_title
Bob,Alice,Web Site
Bob,Charlie,Web Application
Sayı verilen isimler için Kullanıcı masa aramak ve seçmek için gerekli olacağını bu yüzden, gözetmen veya öğrenci isimleri ziyade onların kimliklerini tutmaz Eşleştirme tablodur kimlikleri daha sonra bu kimlikleri ve verilen proje başlığını kullanarak Eşleştirme'yi oluşturur.
Aşağıdaki kod bana hata bir çok yönlendirmeleri veren ve eşleştirmeleri tabloya boş kayıt ekleme edilir.
Pairing.rb
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
supervisorName = row[0]
studentName = row[1]
title = row [2]
supervisorID = User.select(:id).where(name: supervisorName)
studentID = User.select(:id).where(name: studentName)
pair = Pairing.new
pair.supervisor_id = supervisorID
pair.student_id = studentID
pair.project_title = title
pair.save
end
end
Pairings_controller.rb
def new
@pairing = Pairing.new
end
def create
@pairing = Pairing.new(pairing_params)
if @pairing.save
redirect_to pairings_path, :notice => "Pairing Successful!"
else
redirect_to pairings_path, :notice => "Pairing Failed!"
end
end
def import
Pairing.import(params[:file])
redirect_to pairings_path, :notice => "Pairs Imported"
end
, yönlendirir. Teşekkürler ! – Yoklan