因此,我的作业要求我生成一个页面来处理学生,课程和部分。我应该使用many_to_many或类似的方法来列出所有共享数据。我的具体问题是,我创建了一个似乎无效的新表(rails g scaffold enrolling students:references section:references)。当我尝试使用学生和部分创建新的注册时,出现错误,指出“部分必须存在” error。我不知道这个错误是从哪里来的。此视图中的sections字段仅填充有现有的部分,因此它说“必须存在”的事实非常令人困惑。谁能指出我正确的方向来解决这个问题?我已经尝试过不同的方法来重建这个项目3次,只是...卡住了。我将发布相关代码,但是如果我对您可能需要查看的部分有误,我会很乐意发布其余部分。
class Enrollment < ApplicationRecord
belongs_to :section
belongs_to :student
end
<%= form_with(model: enrollment, local: true) do |form| %>
<% if enrollment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(enrollment.errors.count, "error") %>
prohibited this enrollment from being saved:</h2>
<ul>
<% enrollment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :student_id %>
<%= form.collection_select :student_id, Student.order(:student_name), :id,
:student_id, include_blank:true %>
</div>
<div class="field">
<%= form.label :course_id %>
<%= form.collection_select :course_id, Course.order(:name), :id, :name,
include_blank: true %>
</div>
<div class="field">
<%= form.label :sections_number %>
<%= form.collection_select :section_number, Section.all, :id,
:section_number, include_blank:false %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
class Section < ApplicationRecord
has_many :enrollments
has_and_belongs_to_many :students, through: :enrollments
belongs_to :course
def numsem
"#{course.name} #{course_id}"
end
end
L
-----------------------编辑添加控制器代码----------------
class EnrollmentsController < ApplicationController
before_action :set_enrollment, only: [:show, :edit, :update, :destroy]
# GET /enrollments
# GET /enrollments.json
def index
@enrollments = Enrollments.all
end
# GET /enrollments/1
# GET /enrollments/1.json
def show
end
# GET /enrollments/new
def new
@enrollment = Enrollments.new
@sections = Section.all
@students = Student.all
end
# GET /enrollments/1/edit
def edit
end
# POST /enrollments
# POST /enrollments.json
def create
@enrollment = Enrollments.new(enrollment_params)
respond_to do |format|
if @enrollment.save
format.html { redirect_to @enrollment, notice: 'Enrollments was
successfully created.' }
format.json { render :show, status: :created, location: @enrollment }
else
format.html { render :new }
format.json { render json: @enrollment.errors, status:
:unprocessable_entity }
end
end
end
# PATCH/PUT /enrollments/1
# PATCH/PUT /enrollments/1.json
def update
respond_to do |format|
if @enrollment.update(enrollment_params)
format.html { redirect_to @enrollment, notice: 'Enrollments was
successfully updated.' }
format.json { render :show, status: :ok, location: @enrollment }
else
format.html { render :edit }
format.json { render json: @enrollment.errors, status:
:unprocessable_entity }
end
end
end
# DELETE /enrollments/1
# DELETE /enrollments/1.json
def destroy
@enrollment.destroy
respond_to do |format|
format.html { redirect_to enrollments_index_url, notice: 'Enrollments was
successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_enrollment
@enrollment = Enrollments.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list
through.
def enrollment_params
params.require(:enrollment).permit(:student_id, :section_id)
end
end
答案 0 :(得分:0)
该部分必须存在
该错误是因为您尝试使用 nil @enrollment
保存section_id
并引发该异常。问题出在您的表单上
<%= form.collection_select :section_number, Section.all, :id,
:section_number, include_blank:false %>
应该是
<%= form.collection_select :section_id, Section.all, :id,
:section_number, include_blank:false %>