ActiveRecord::InvalidForeignKey in ArticlesController#destroy
SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM "articles" WHERE "articles"."id" = ?
我創建一個博客的應用程序,每次我試圖刪除它有評論文章時出現此錯誤。我該如何解決它?的Rails的ActiveRecord :: InvalidForeignKey在ArticlesController#破壞
讓我知道什麼代碼發佈,我會更新問題。
文章控制器:
class ArticlesController < ApplicationController
def new
@article = Article.new
end
def index
#@articles = Article.all
@articles = Article.paginate(:page => params[:page], :per_page => 10)
end
def show
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
end
private
def article_params
params.require(:article).permit(:title, :text, :datee)
end
文章型號:
class Article < ApplicationRecord
has_many :comments
has_many :photos
end
評價模型:
class Comment < ApplicationRecord
belongs_to :article
end
UPDATE
現在我有一個新的錯誤
ArgumentError in ArticlesController#destroy
Unknown key: :dependant. Valid keys are: :class_name, :anonymous_class, :foreign_key, :validate, :autosave, :table_name, :before_add, :after_add, :before_remove, :after_remove, :extend, :primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache, :join_table, :foreign_type, :index_errors
謝謝,但我有一個新的錯誤 – bockdavidson
@bockdavidson對不起,我有一個錯字;請檢查更新的答案(它應該是「依賴」,而不是「依賴」)。 – Gerry
它工作!非常感謝! – bockdavidson