lundi 16 novembre 2020

SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM "courses" WHERE "courses"."Id"

routes.rb
    Rails.application.routes.draw do
          get "students" => "students#index"
          get "students/new" => "students#new"
          post "students" => "students#create"
        
          get "courses" => "courses#index"
          get "courses/new" => "courses#new"
          post "courses" => "courses#create"
        
          get "students/:id" => "students#show"
          get "courses/:id/students" => "courses#students"
          get "courses/:id/assignments" => "courses#assignments"
          get "courses/:id/assignments/new" => "assignments#new"
          post "courses/:id/assignments" => "assignments#create"
        
          get "courses/:id/edit" => "courses#edit"
          patch "courses/:id" => "courses#update"
          delete "courses/:id" => "courses#destroy"
        
        get "students/:id/edit" => "students#edit"
        patch "students/:id" => "students#update"
        delete "students/:id" => "students#destroy" 
        end 

CourseController.rb
        class CoursesController < ApplicationController
        def index
        @courses = Course.all
        end
        
        def new
        end
        
        def create
        course = Course.new(course_params)
        if course.save
        redirect_to "/courses"
        else
        flash[:errors] = course.errors.full_messages
        redirect_to "/courses/new"
        end
        end
        
        def students
        @students = Course.find(params[:id]).students
        end 
        
        def assignments
        @assignments = Course.find(params[:id]).assignments
        end
        
        def edit
        @course = Course.find(params[:id])
        end
        
        def update
        course = Course.find(params[:id])
        if course.update(course_params)
        redirect_to "/courses"
        else
        flash[:errors] = course.errors.full_messages
        redirect_to "/courses/#{course.id}/edit"
        end
        end
        
        def destroy
        @assignments = Course.find(params[:id])
        @assignments.destroy                      //This line is showing me an error
        redirect_to "/courses/new"
        end
        
        private
        def course_params`enter code here`
        params.require(:course).permit(:title, :professor, :room)
        end
        end

My delete function is not working in this code. When I am trying to run this code it shows the following error what is show in the title. please can someone help me to solve this question thank you. I will also highlight the line whatever line shows the error it is just one line. I do not I think it coding thing is right. but, I guess I am missing some inside the code or probably on routes.rb

Aucun commentaire:

Enregistrer un commentaire