Rails 中的浅嵌套 - RecordNotFound 错误

Shallow Nesting in Rails - RecordNotFound Error

我试图找出 Rails 中的浅层嵌套,但我得到了某些操作的 RecordNotFound。一个学生有很多清单,一个清单属于一个学生,我确保 routes.rb 设置正确并且 form_with 部分让学生和清单都通过了。

我觉得我的控制器有问题:

class ChecklistsController < ApplicationController
  before_action :get_student
  before_action :set_checklist, only: %i[show edit update destroy]

  def index
    @checklists = @student.checklists
  end
  def show
    @student = @checklist.student
  end
  def new
    @checklist = @student.checklists.build
  end
  def edit
    @student = @checklist.student
  end
  def create
    @checklist = @student.checklists.build(checklist_params)

    if @checklist.save
      redirect_to @checklist, notice: 'Checklist was successfully created.'
    else
      render :new, status: :unprocessable_entity
    end
  end
  def update
    @student = @checklist.student

    if @checklist.update(checklist_params)
      redirect_to @checklist, notice: 'Checklist was successfully updated.'
    else
      render :edit, status: :unprocessable_entity
    end
  end
  def destroy
    @student = @checklist.student

    @checklist.destroy
    redirect_to student_checklists_url(@student), notice: 'Checklist was successfully destroyed.'
  end

  private

  def set_checklist
    @checklist = @student.checklists.find(params[:id])
  end

  def get_student
    @student = Student.find(params[:student_id])
  end

  # Only allow a list of trusted parameters through.
  def checklist_params
    params.require(:checklist).permit(:title, :date, :setting)
  end
end

使用浅嵌套时,您需要将嵌套的集合操作(​​新建、创建、索引)的回调与非嵌套的成员操作(显示、编辑、更新、销毁)分开:

class ChecklistsController < ApplicationController
  before_action :set_student, only: %i[new, index, create]
  before_action :set_checklist, only: %i[show edit update destroy]

  def index
    @checklists = @student.checklists
  end

  # Defining this empty method is actually optional - Rails will implicitly render `show.html.erb` anyways
  def show
  end

  def new
    @checklist = @student.checklists.build
  end

  def create
    @checklist = @student.checklists.build(checklist_params)
    if @checklist.save
      redirect_to @checklist, notice: 'Checklist was successfully created.'
    else
      render :new, status: :unprocessable_entity
    end
  end

  def edit
  end

  def update
    if @checklist.update(checklist_params)
      redirect_to @checklist, notice: 'Checklist was successfully updated.'
    else
      render :edit, status: :unprocessable_entity
    end
  end

  def destroy
    @checklist.destroy
    redirect_to student_checklists_url(@student), notice: 'Checklist was successfully destroyed.'
  end


  private

  def set_student
    @student = Student.find(params[:student_id])
  end

  def set_checklist
    # this lookup will never be based off a student record
    # since the route is not nested 
    @checklist = Checklist.eager_load(:student)
                          .find(params[:id])
    @student = @checklist.student
  end

  # Only allow a list of trusted parameters through.
  def checklist_params
    params.require(:checklist)
          .permit(:title, :date, :setting)
  end
end