我有一个PagesController
,我正在创建一个这样的新记录:
def new
@page = Page.create(status: "draft", title: "Temporary title")
end
我正在使用Page.create
因为用户应该能够立即add page_items
到新页面,并且关联需要带有ID的记录。
问题在于,每当我点击“新记录”时,我都会创建两条新记录。不只是一个。
修改:此问题似乎与Turbolinks有关。如果我使用普通的“新页面”链接,则会向服务器发出两个请求,因此会创建两个页面。如果我手动访问链接或在新选项卡中打开链接,我会得到一条记录。
这个问题贯穿整个应用。所有链接都会向服务器创建两个get请求。
编辑2:这是我点击“新页面”链接时的服务器响应:
Started GET "/pages/new" for 127.0.0.1 at 2014-02-28 22:39:41 +0100
Processing by PagesController#new as HTML
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."auth_token" = '040__V_8SbdLhYxbjd1sYQ' LIMIT 1
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."subdomain" = 'ohenrik' LIMIT 1
Page Load (0.3ms) SELECT "pages".* FROM "pages" WHERE "pages"."id" IN (3)
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO "pages" ("created_at", "status", "title", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Fri, 28 Feb 2014 21:39:41 UTC +00:00], ["status", "draft"], ["title", "Temporary title"], ["updated_at", Fri, 28 Feb 2014 21:39:41 UTC +00:00]]
(1.1ms) COMMIT
Rendered shared/_form_errors.html.erb (0.0ms)
Rendered pages/_rich_link_button.erb (0.0ms)
Rendered pages/_rich_content_toolbar.erb (0.6ms)
Rendered pages/_rich_link_button.erb (0.0ms)
Rendered pages/_rich_excerpt_toolbar.erb (0.5ms)
PageItem Load (0.3ms) SELECT "page_items".* FROM "page_items" WHERE "page_items"."page_id" = $1 ORDER BY (case when page_items.ancestry is null then 0 else 1 end), page_items.ancestry, position [["page_id", 55]]
Rendered pages/_form.html.erb (5.6ms)
Rendered pages/new.html.erb within layouts/application (6.1ms)
Rendered shared/_navigation.html.erb (27.0ms)
Rendered shared/_notice.html.erb (0.1ms)
Completed 200 OK in 53ms (Views: 46.6ms | ActiveRecord: 3.4ms)
Started GET "/pages/new" for 127.0.0.1 at 2014-02-28 22:39:42 +0100
Processing by PagesController#new as HTML
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."auth_token" = '040__V_8SbdLhYxbjd1sYQ' LIMIT 1
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."subdomain" = 'ohenrik' LIMIT 1
Page Load (0.3ms) SELECT "pages".* FROM "pages" WHERE "pages"."id" IN (3)
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO "pages" ("created_at", "status", "title", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Fri, 28 Feb 2014 21:39:42 UTC +00:00], ["status", "draft"], ["title", "Temporary title"], ["updated_at", Fri, 28 Feb 2014 21:39:42 UTC +00:00]]
(0.3ms) COMMIT
Rendered shared/_form_errors.html.erb (0.1ms)
Rendered pages/_rich_link_button.erb (0.0ms)
Rendered pages/_rich_content_toolbar.erb (0.6ms)
Rendered pages/_rich_link_button.erb (0.0ms)
Rendered pages/_rich_excerpt_toolbar.erb (0.4ms)
PageItem Load (0.3ms) SELECT "page_items".* FROM "page_items" WHERE "page_items"."page_id" = $1 ORDER BY (case when page_items.ancestry is null then 0 else 1 end), page_items.ancestry, position [["page_id", 56]]
Rendered pages/_form.html.erb (4.9ms)
Rendered pages/new.html.erb within layouts/application (5.3ms)
Rendered shared/_navigation.html.erb (41.1ms)
Rendered shared/_notice.html.erb (0.1ms)
Completed 200 OK in 66ms (Views: 60.5ms | ActiveRecord: 2.6ms)
注意,删除了一些额外不必要的代码以使问题更清晰。问题与一个已包含jquery的额外jquery插件有关。这导致远程链接(也是turbolink)被解雇两次。我的回答如下:
对可能出错的任何提示或答案表示赞赏! :)
答案 0 :(得分:0)
使用find_or_create_*
方法代替create
。
def new
@page = Page.find_or_create_by_status_and_title("draft","Temporary title")
end
唯一可能的原因是您的控制器可能会收到两个不同的请求。可能存在一些客户端错误,例如javascript正在重新加载所有页面,或者用户多次单击该按钮。您可以检查日志并确认。