【问题标题】:Rails action being called multiple times per request每个请求多次调用 Rails 操作
【发布时间】:2017-08-31 20:32:51
【问题描述】:

我有一个控制器操作,每个浏览器请求都会调用 3 次。有任何想法吗? config/routes.rb 和 application_controller.rb 非常简单。

代码如下,

config/routes.rb

Rails.application.routes.draw do
    root 'tiles#index'

+

controllers/tiles_controller.rb

class TilesController < ApplicationController
def index
  puts "this line prints three times in the console per request"
end

+

tiles/index.html.erb
<% if notice %>
  <p id="notice"><%= notice %></p>
<% end %>

<div id="tiles">
  <%= render "tile", tile: { type: "sam", id: "0" } %>
  <%= render "tile", tile: { type: "inputs", id: "1" } %>
  <%= render collection: @tiles, partial: "tile" %>
  <%= render "tile", tile: { type: "social", id: "1000" } %>
</div>

+

这是控制台日志:

log/development.log
Started GET "/" for 127.0.0.1 at 2017-08-31 16:56:28 -0400
Processing by TilesController#index as HTML
Started GET "/" for 127.0.0.1 at 2017-08-31 16:56:28 -0400
Processing by TilesController#index as HTML
  [1m[36mProject Load (0.5ms)[0m  [1m[34mSELECT  "projects".* FROM "projects" ORDER BY "projects"."launch_date" ASC LIMIT $1[0m  [["LIMIT", 10]]
  [1m[36mProject Load (9.1ms)[0m  [1m[34mSELECT  "projects".* FROM "projects" ORDER BY "projects"."launch_date" ASC LIMIT $1[0m  [["LIMIT", 10]]
  Rendering tiles/index.html.erb within layouts/application
  Rendered tiles/_tile_sam.html.erb (0.3ms)
  Rendered tiles/_tile.html.erb (8.0ms)
  Rendering tiles/index.html.erb within layouts/application
  Rendered tiles/_tile_sam.html.erb (0.0ms)
  Rendered tiles/_tile.html.erb (0.9ms)
  Rendered tiles/_tile_instagram.html.erb (12.6ms)  
  ...
  Rendered tiles/_tile_twitter.html.erb (0.5ms)
  Rendered collection of tiles/_tile.html.erb [48 times] (125.9ms)
  Rendered tiles/_tile_project.html.erb (0.1ms)
  Rendered tiles/_tile_social.html.erb (0.6ms)
  Rendered tiles/_tile.html.erb (2.7ms)
  Rendered tiles/index.html.erb within layouts/application (166.1ms)
  Rendered tiles/_tile_twitter.html.erb (0.6ms)
  ...
  Rendered collection of tiles/_tile.html.erb [48 times] (158.5ms)
  Rendered tiles/_tile_social.html.erb (0.1ms)
  Rendered tiles/_tile.html.erb (1.0ms)
  Rendered tiles/index.html.erb within layouts/application (165.3ms)
Completed 200 OK in 1310ms (Views: 217.1ms | ActiveRecord: 9.1ms)


Completed 200 OK in 1325ms (Views: 204.5ms | ActiveRecord: 0.5ms)

救命!

【问题讨论】:

  • 能否展示完整的请求日志和app/views/tiles/index.html.erb代码?
  • @Gerry 现在添加 ...
  • @Gerry 如何找到请求日志?
  • 在您的log/ 文件夹中
  • 这看起来确实很奇怪,但这可能是本地配置的一些怪癖。浏览器通常使用系统首选项进行代理设置,另外还可以安装插件(或恶意软件!?),使其行为异常。我会仔细检查您的设置中没有任何奇怪的地方。他们是否也对外部 URL 提出双重请求?即使在退出或重新启动之后?

标签: ruby-on-rails ruby controller action


【解决方案1】:

哇

最后通过逐行删除与此操作/视图相关的代码来解决这个问题。基本上,我生成了一些img 带有空src 字段的标签。显然这会导致许多现代浏览器多次加载网站,可能是为了寻找丢失的图像或假设根 URL 是图像本身?

找到此处引用的行为:

https://forums.asp.net/t/1804472.aspx?Bizzare+safari+thing+two+page+loads+for+each+page+

page loads twice in Google chrome

【讨论】:

  • @tadman 解释了为什么无法使用 cURL 复制此行为!
  • 是的,它不会加载相关资产。好发现!考虑到您通常需要在启动下一次加载之前完成页面的渲染,我很惊讶这些调用与它们重叠的次数一样多,但看起来 Chrome 真的很想开始。
猜你喜欢
  • 2018-04-18
  • 1970-01-01
  • 2016-05-10
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多