0
我跟隨此tutorial。本教程要求我實現標籤的銷燬功能。在導軌中刪除標籤時無效的外鍵
這些標籤可以創建並顯示正常。然而,毀壞他們產生以下錯誤:
Parameters: {"authenticity_token"=>"VcYU8FRqn4oBXCv0NKXuO7yKNdI+9fIk46rY1ZwD7cQ8cqi37nZDVwNnWJLcNMWVq4gi3OU3YFDgzdeTRa1XKw==",
"id"=>"1"}
Tag Load (0.5ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.0ms) begin transaction
SQL (2.0ms) DELETE FROM "tags" WHERE "tags"."id" = ? [["id", 1]]
(1.0ms) rollback transaction
Completed 500 Internal Server Error in 10ms (ActiveRecord: 3.5ms)
ActiveRecord::InvalidForeignKey (SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM "tags" WHERE "tags"."i
d" = ?):
app/controllers/tags_controller.rb:13:in `destroy
tags_controller.rb
class TagsController < ApplicationController
def index
@tags = Tag.all
end
def show
@tag = Tag.find(params[:id])
end
def destroy
@tags = Tag.find(params[:id])
@tags.destroy
# Set variable to instance of an object of class Article and call .destroy on it.
end
end
index.html.erb
<h1>Listing tags</h1>
<% @tags.each do |tag| %>
<tr>
<td><%= tag.name %></td>
<td><%= link_to 'Show', tag_path(tag) %></td>
<td><%= link_to 'Delete', tag_path(tag),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
是否有一步我錯過了什麼我進錯了?
你可以請加你的模型嗎? –
如果您的標籤有標籤,您必須先刪除所有標籤。你可以用'dependent::destroy'來做到這一點,或者在標籤控制器銷燬行爲中刪除它們。 – Maxence