简体   繁体   中英

Rails turbolink requests are submitted twice

I have a PagesController where I'm creating a new record like this:

  def new
    @page = Page.create(status: "draft", title: "Temporary title")
  end

I'm using Page.create because the user should immediately be able to add page_items to the new page and the associations need a record with an ID.

The problem is that whenever I click "new record" I create two new records. Not just one.

Edit: The problem appears to be related to Turbolinks. If i use the normal "new page" link, two requests are made to the server, so two pages get created. If i visit the link manually or open the link in a new tab i get one record.

This problem is across the whole app. All links create two get requests to the server.

Edit 2: here are the server response when i click on the "new page" link:

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)

Note, removed some of the extra unnecessary code to make the question clearer. the problem was related to an extra jquery plugin that had jquery already included. this resulted in remote links (also turbolink) being fired twice. se my answer bellow

Any hints or answers to what might be wrong is appreciated! :)

use find_or_create_* method instead of create .

def new
  @page = Page.find_or_create_by_status_and_title("draft","Temporary title")
end

The only possible reason is your controller might be getting two different request. There might be some client error like javascript is reloading all page, or user is clicking the button multiple times. You can check logs and confirm it.

Read More here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM