gpt4 book ai didi

ruby-on-rails - 无法删除或更新父行 : a foreign key constraint fails - deleting a post in blog

转载 作者:行者123 更新时间:2023-12-04 05:40:59 25 4
gpt4 key购买 nike

我已经检查了类似问题的其他答案,我想我理解它们的意思,但我不知道如何解决它。

我在尝试删除我网站上博客中的帖子时收到此错误消息。我已经从 Rails 控制台尝试过,也得到了类似的消息。

ActiveRecord::StatementInvalid in PostsController#destroy
Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails (`mysite_development`.`comments`, CONSTRAINT `fk_rails_2fd19c0db7` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)): DELETE FROM `posts` WHERE `posts`.`id` = 3

post_controller.rb
class PostsController < ApplicationController
before_filter :authenticate, :except => [ :index, :show ]
before_action :set_post, only: [:show, :edit, :update, :destroy]

# GET /posts
# GET /posts.json
def index
@posts = Post.all
end

# GET /posts/1
# GET /posts/1.json
def show
end

# GET /posts/new
def new
@post = Post.new
end

# GET /posts/1/edit
def edit
end

# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)

respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }

end

end

private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :body)
end
end

编辑.html.erb
<h1>Editing Post</h1>

<%= render 'form' %>

<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
<%= link_to 'Delete', @post, method: :delete %>

_form.html.erb
<%= simple_form_for(@post) do |f| %>
<%= f.error_notification %>

<div class="form-inputs">
<%= f.input :title %>
<%= f.input :body %>
</div>

<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>

有任何想法吗?

最佳答案

它与您的 Controller 代码无关。您添加了 database foreign constraing在您的架构中,当有关联的记录时,它不允许您删除对象。

在这种情况下,您试图删除 PostComments随附的。您需要更改架构并更新外键定义以指示数据库在这种情况下要做什么。具体来说,您可能希望删除级联上的关联记录。

理论上,您可以使用 dependent: :destroy Post 中的 Rails 设置模型删除Comment s 级联,但这不是一个好主意,因为你有一个外键。在这种情况下,如果将任务委托(delegate)给数据库会更快更好。

关于ruby-on-rails - 无法删除或更新父行 : a foreign key constraint fails - deleting a post in blog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34229264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com