gpt4 book ai didi

ruby-on-rails - FriendlyID unique slugs by scope 和 active admin

转载 作者:行者123 更新时间:2023-11-29 12:21:22 24 4
gpt4 key购买 nike

我有一个模特秀,我有一个模特插曲。正如您想象的那样,一个节目有很多集。我已经在剧集模型中配置了 friendlyID:

friendly_id :slug_candidates, use: :slugged, :use => :scoped, :scope => :show

我想我遇到的问题是由于我没有使用 Active Admin 中的资源范围并且没有自定义嵌套资源检索,但我不确定该怎么做:这是发生了什么。

假设我有一个名为“Show 1”的节目,我创建了一个以“Episode 1”为标题的剧集。当我创建一个新节目并在“节目 2”中包含“第 1 集”时,它应该能够保存,因为它的范围仅限于节目标题。

2014-02-07T00:38:28.009413+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"yhP8K6jUVhWHHzqWhXvaABXDzh8eACYcHhkEhMKJt+8=", "show"=>{"creator_id"=>"132", "title"=>"Menuda Noche", "subtitle"=>"", "description"=>"", "position"=>"", "age_range"=>"", "single"=>"0", "skiplist"=>"0", "promote"=>"0", "approved"=>"1", "created_at(1i)"=>"", "created_at(2i)"=>"", "created_at(3i)"=>"", "created_at(4i)"=>"", "created_at(5i)"=>"", "updated_at(1i)"=>"", "updated_at(2i)"=>"", "updated_at(3i)"=>"", "updated_at(4i)"=>"", "updated_at(5i)"=>"", "episodes_attributes"=>{"1391733477006"=>{"episode"=>"1", "title"=>"Episode 1", "description"=>"", "duration"=>"6:11", "age_range"=>"", "embed_id"=>"3", "video"=>"b3f85199", "approved"=>"1", "tag_list"=>""}}, "channel_ids"=>["", "3"]}, "commit"=>"Create Show"}
2014-02-07T00:38:28.089273+00:00 app[web.1]: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_episodes_on_slug"
2014-02-07T00:38:28.089273+00:00 app[web.1]: DETAIL: Key (slug)=(episode-1) already exists.
2014-02-07T00:38:28.093312+00:00 app[web.1]: Completed 500 Internal Server Error in 84ms
2014-02-07T00:38:28.099544+00:00 app[web.1]: ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_episodes_on_slug"

然后我的问题是:我必须在 admin/show.rb 或 admin/episode.rb 中做什么,才能不仅允许保存具有已在节目中使用过的标题的剧集,而且对于该节目来说是独一无二的,以及如何检索它?

更新

我仍在尝试找出应该在哪里重写创建/更新方法;因为我正在使用 admin/show Controller 来创建一个新的节目和嵌套的剧集,我猜我必须在那里覆盖它,但我仍然不清楚这一点:

在 admin/show.rb 中,我把它放在:

controller do
def find_resource
scoped_collection.friendly.find(params[:id])
end
def create
puts " ACCCCCTTTIVE AADDDDMMMIIIN CREEEEATTTTTTE "
params.inspect
end
end

这当然会阻止任何保存,但我认为这将是一个很好的愚蠢测试。然后我将其注释掉并在 admin/episode.rb 中添加了类似的东西,但它似乎从未被调用——让我认为 episode.rb 保存功能不会在那里发生......

Started POST "/admin/shows" for 127.0.0.1 at 2014-02-06 23:03:57 -0500
Processing by Admin::ShowsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"S5sXpSWZCLG3lg3x2QSw9p1xuHyGxIJndAqYWQWrC40=", "show"=>{"creator_id"=>"1", "title"=>"new new new", "subtitle"=>"sdf", "description"=>"", "position"=>"", "age_range"=>"", "single"=>"0", "skiplist"=>"0", "promote"=>"0", "approved"=>"0", "created_at(1i)"=>"", "created_at(2i)"=>"", "created_at(3i)"=>"", "created_at(4i)"=>"", "created_at(5i)"=>"", "updated_at(1i)"=>"", "updated_at(2i)"=>"", "updated_at(3i)"=>"", "updated_at(4i)"=>"", "updated_at(5i)"=>"", "episodes_attributes"=>{"1391745822502"=>{"episode"=>"", "title"=>"Part 1", "description"=>"", "duration"=>"", "age_range"=>"", "embed_id"=>"1", "video"=>"1234", "approved"=>"0", "tag_list"=>""}}, "channel_ids"=>[""]}, "commit"=>"Create Show"}
AdminUser Load (38.1ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = 1 ORDER BY "admin_users"."id" ASC LIMIT 1
(149.2ms) BEGIN
Show Exists (18.7ms) SELECT 1 AS one FROM "shows" WHERE "shows"."slug" = 'new-new-new' LIMIT 1
Episode Exists (23.1ms) SELECT 1 AS one FROM "episodes" WHERE "episodes"."show_id" IS NULL AND "episodes"."slug" = 'part-1' LIMIT 1
SQL (205.8ms) INSERT INTO "shows" ("age_range", "approved", "created_at", "creator_id", "description", "promote", "single", "skiplist", "slug", "subtitle", "title", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["age_range", ""], ["approved", false], ["created_at", Thu, 06 Feb 2014 23:03:57 EST -05:00], ["creator_id", 1], ["description", ""], ["promote", false], ["single", false], ["skiplist", false], ["slug", "new-new-new"], ["subtitle", "sdf"], ["title", "new new new"], ["updated_at", Thu, 06 Feb 2014 23:03:57 EST -05:00]]
SQL (22.4ms) INSERT INTO "episodes" ("age_range", "approved", "created_at", "description", "duration", "embed_id", "show_id", "slug", "title", "updated_at", "video") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id" [["age_range", ""], ["approved", false], ["created_at", Thu, 06 Feb 2014 23:03:57 EST -05:00], ["description", ""], ["duration", ""], ["embed_id", "1"], ["show_id", 188], ["slug", "part-1"], ["title", "Part 1"], ["updated_at", Thu, 06 Feb 2014 23:03:57 EST -05:00], ["video", "1234"]]
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_episodes_on_slug"
DETAIL: Key (slug)=(part-1) already exists.
: INSERT INTO "episodes" ("age_range", "approved", "created_at", "description", "duration", "embed_id", "show_id", "slug", "title", "updated_at", "video") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id"
(1.6ms) ROLLBACK
Completed 500 Internal Server Error in 573ms

PG::UniqueViolation - ERROR: duplicate key value violates unique constraint "index_episodes_on_slug"
DETAIL: Key (slug)=(part-1) already exists.

更新#2

还是有点糊涂;我可以看到它在 Controller do/def create 中的 admin/show 内中断,但我无法弄清楚如何最好地停止保存节目和嵌套剧集的自动过程。

Show Exists (4.3ms)  SELECT 1 AS one FROM "shows" WHERE "shows"."slug" = 'blah-black' LIMIT 1
Episode Exists (2.5ms) SELECT 1 AS one FROM "episodes" WHERE "episodes"."show_id" IS NULL AND "episodes"."slug" = 'part-1' LIMIT 1
Episode Exists (2.9ms) SELECT 1 AS one FROM "episodes" WHERE "episodes"."show_id" IS NULL AND "episodes"."slug" = 'part-2' LIMIT 1

由于剧集正在放映中,所以看起来这应该是不同的,因为没有其他剧集名称具有空 ID。

我唯一能想到的另一件事是手动执行此操作:首先保存节目,获取 show_id,然后尝试使用该 show_id 保存每个嵌套剧集,但不确定我是否正确地考虑了这一点......

最佳答案

PG::UniqueViolation - ERROR:  duplicate key value violates unique constraint "index_episodes_on_slug"

看起来这个错误来自您的数据库,而不是活跃的管理员。您可能需要更改迁移中的唯一索引以包含 show_id。

因此在您的迁移中而不是:

add_index :episodes, :slug, :unique => true

你应该有类似的东西

add_index :episodes, [:show_id, :slug], :unique => true

关于ruby-on-rails - FriendlyID unique slugs by scope 和 active admin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21617680/

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