FriendlyID独特的slu its范围和主动管理员

时间:2014-02-07 01:32:18

标签: ruby-on-rails postgresql activeadmin friendly-id

我有一个模特秀,我有一个模特集。你可以想象一个节目有很多集。我在剧集模型中配置了friendlyID:

friendly_id:slug_candidates,使用:: slugged,:use => :scoped,:scope => :显示

我想我遇到的问题是由于我没有使用Active Admin中的资源范围而没有自定义嵌套资源检索,但我不确定如何做到这一点:这就是发生的事情。

假设我有一个名为'Show 1'的节目,我创作了一集以'Episode 1'为标题。当我创建一个新节目并在'Show 2'中有'Episode 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中我必须做什么,不仅允许在节目中保存已经使用过的标题的剧集,而且该节目是独一无二的,以及如何检索它?

更新

我还在试图找出应该覆盖create / update方法的地方;因为我正在使用admin / show控制器创建一个新节目和嵌套剧集,我猜我必须在那里覆盖它,但我仍然不清楚:

在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

还是有点困惑;我可以看到它在控制器do / def创建中的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保存每个嵌套剧集,但不确定我是否正确地考虑了这个...

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您是否考虑过使用belongs_to

应用程序/管理/ shows.rb:

ActiveAdmin.register Show do
  ...
end

应用程序/管理/ episodes.rb:

ActiveAdmin.register Episode do
  belongs_to :show
  ...
end

ActiveAdmin将为您的资源注册嵌套路由,ala:

http://server.com/shows/1/episodes/2

答案 2 :(得分:0)

应用程序/模型/ episode.rb:

class Episode < ActiveRecord::Base
  ....
  friendly_id :title, :use => [:slugged, :scoped], :scope => :show
  ....
end

应用程序/管理/ episodes.rb:

ActiveAdmin.register Episode do
  belongs_to :show, :optional => true
  ...
end

对我来说很好。