我有在轨道上的乘客/ nginx的与分配给它的3个进程运行(4.1.14)的应用程序内部的红宝石。非常偶尔(< 5%的时间)我从单个提交操作中获得多个POST。以下是日志的样子:
1)用户访问使用Google Chrome的build_item_interaction#new。这产生3(!)GET请求。
I, [2017-02-14T13:27:49.679558 #19777] INFO -- : Started GET "/build_item_interactions/new?serial_number=1702R022&type=end_of_line" for 192.168.45.105 at 2017-02-14 13:27:49 -0600
I, [2017-02-14T13:27:49.681263 #19777] INFO -- : Processing by BuildItemInteractionsController#new as HTML
I, [2017-02-14T13:27:49.681354 #19777] INFO -- : Parameters: {"serial_number"=>"1702R022", "type"=>"end_of_line"}
I, [2017-02-14T13:27:49.690776 #19777] INFO -- : Rendered build_item_interactions/_new_interaction.html.erb (4.7ms)
I, [2017-02-14T13:27:49.690956 #19777] INFO -- : Rendered build_item_interactions/new.html.erb within layouts/application (5.1ms)
I, [2017-02-14T13:27:49.693310 #19777] INFO -- : Rendered layouts/_login_header.html.erb (1.4ms)
I, [2017-02-14T13:27:49.693779 #19777] INFO -- : Completed 200 OK in 12ms (Views: 8.6ms | ActiveRecord: 1.0ms)
I, [2017-02-14T13:27:49.712790 #19777] INFO -- : Started GET "/build_item_interactions/new?serial_number=1702R022&type=end_of_line" for 192.168.45.105 at 2017-02-14 13:27:49 -0600
I, [2017-02-14T13:27:49.714741 #19777] INFO -- : Processing by BuildItemInteractionsController#new as HTML
I, [2017-02-14T13:27:49.714852 #19777] INFO -- : Parameters: {"serial_number"=>"1702R022", "type"=>"end_of_line"}
I, [2017-02-14T13:27:49.722208 #19777] INFO -- : Rendered build_item_interactions/_new_interaction.html.erb (3.5ms)
I, [2017-02-14T13:27:49.722370 #19777] INFO -- : Rendered build_item_interactions/new.html.erb within layouts/application (3.8ms)
I, [2017-02-14T13:27:49.724351 #19777] INFO -- : Rendered layouts/_login_header.html.erb (1.2ms)
I, [2017-02-14T13:27:49.724693 #19777] INFO -- : Completed 200 OK in 10ms (Views: 6.4ms | ActiveRecord: 0.7ms)
I, [2017-02-14T13:27:49.746169 #19777] INFO -- : Started GET "/build_item_interactions/new?serial_number=1702R022&type=end_of_line" for 192.168.45.105 at 2017-02-14 13:27:49 -0600
I, [2017-02-14T13:27:49.747577 #19777] INFO -- : Processing by BuildItemInteractionsController#new as HTML
I, [2017-02-14T13:27:49.747658 #19777] INFO -- : Parameters: {"serial_number"=>"1702R022", "type"=>"end_of_line"}
I, [2017-02-14T13:27:49.754470 #19777] INFO -- : Rendered build_item_interactions/_new_interaction.html.erb (3.6ms)
I, [2017-02-14T13:27:49.754647 #19777] INFO -- : Rendered build_item_interactions/new.html.erb within layouts/application (3.9ms)
I, [2017-02-14T13:27:49.756809 #19777] INFO -- : Rendered layouts/_login_header.html.erb (1.3ms)
I, [2017-02-14T13:27:49.757209 #19777] INFO -- : Completed 200 OK in 9ms (Views: 6.9ms | ActiveRecord: 0.7ms)
2)然后,用户点击一个派生连续2所POST请求的页面上的[提交]按钮:
I, [2017-02-14T13:27:59.692934 #19777] INFO -- : Started POST "/build_item_interactions?serial_number=1702R022&type=end_of_line" for 192.168.45.105 at 2017-02-14 13:27:59 -0600
I, [2017-02-14T13:27:59.693996 #19777] INFO -- : Processing by BuildItemInteractionsController#create as HTML
I, [2017-02-14T13:27:59.694105 #19777] INFO -- : Parameters: {"utf8"=>"?", "authenticity_token"=>"FFs8Uh07oKpvZ57wHzTll/PQxVp0eaK3bVIsOKUDGPU=", "build_item_interaction"=>{"build_item_id"=>"4501
", "interaction_type_id"=>"5", "data"=>"", "badge"=>["SuxY3ATMMjs"]}, "commit"=>"Save Checkpoint", "serial_number"=>"1702R022", "type"=>"end_of_line"}
I, [2017-02-14T13:27:59.708826 #19777] INFO -- : Redirected to http://fbdbms/build_items/4501
I, [2017-02-14T13:27:59.709030 #19777] INFO -- : Completed 302 Found in 15ms (ActiveRecord: 7.0ms)
I, [2017-02-14T13:27:59.733202 #19777] INFO -- : Started POST "/build_item_interactions?serial_number=1702R022&type=end_of_line" for 192.168.45.105 at 2017-02-14 13:27:59 -0600
I, [2017-02-14T13:27:59.734318 #19777] INFO -- : Processing by BuildItemInteractionsController#create as HTML
I, [2017-02-14T13:27:59.734412 #19777] INFO -- : Parameters: {"utf8"=>"?", "authenticity_token"=>"FFs8Uh07oKpvZ57wHzTll/PQxVp0eaK3bVIsOKUDGPU=", "build_item_interaction"=>{"build_item_id"=>"4501
", "interaction_type_id"=>"5", "data"=>"", "badge"=>["SuxY3ATMMjs"]}, "commit"=>"Save Checkpoint", "serial_number"=>"1702R022", "type"=>"end_of_line"}
I, [2017-02-14T13:27:59.747852 #19777] INFO -- : Redirected to http://fbdbms/build_items/4501
I, [2017-02-14T13:27:59.748100 #19777] INFO -- : Completed 302 Found in 14ms (ActiveRecord: 5.7ms)
3)创建了两个记录,然后用户被转发到资源“所有者” 页:
I, [2017-02-14T13:27:59.756761 #19777] INFO -- : Started GET "/build_items/4501" for 192.168.45.105 at 2017-02-14 13:27:59 -0600
I, [2017-02-14T13:27:59.757784 #19777] INFO -- : Processing by BuildItemsController#show as HTML
I, [2017-02-14T13:27:59.757850 #19777] INFO -- : Parameters: {"id"=>"4501"}
I, [2017-02-14T13:27:59.809268 #19777] INFO -- : Rendered shared/_rfid_scans_table.html.erb (0.1ms)
I, [2017-02-14T13:27:59.809422 #19777] INFO -- : Rendered build_items/show.html.erb within layouts/application (41.4ms)
I, [2017-02-14T13:27:59.811722 #19777] INFO -- : Rendered layouts/_login_header.html.erb (1.3ms)
I, [2017-02-14T13:27:59.812213 #19777] INFO -- : Completed 200 OK in 54ms (Views: 12.3ms | ActiveRecord: 35.6ms)
控制器相关的代码如下所示:
class BuildItemInteractionsController < ApplicationController
def new
load_build_item
load_interaction_info
if @interaction_info then
@interaction = BuildItemInteraction.new(build_item_id: @build_item.try(:id),
interaction_type_id: @interaction_info[:id])
end
end
def create
load_build_item
load_interaction_info
badge = params[:build_item_interaction][:badge]
user = Badge.valid_badge(badge)
@interaction = BuildItemInteraction.new(params_for_interaction)
@interaction.user = user
if @interaction.save then
flash[:success] = "Record saved"
redirect_to @interaction.build_item
else
err = @interaction.errors.full_messages
flash.now[:danger] = "Could not save record: #{err}"
render "new"
end
end
private
def load_interaction_info
@interactions = { "teardown" => { id: 9, description: "End of Teardown" },
"sta4" => { id: 6, description: "End of Station 4" },
"sta7" => { id: 7, description: "End of Station 7" },
"sta8" => { id: 8, description: "End of Test" },
"end_of_line" => { id: 5, description: "End of Line" } }
interaction_type = params[:type]
@interaction_info = @interactions[interaction_type]
end
def load_build_item
sn = params[:serial_number]
@build_item = BuildItem.where(serial_number: sn).first
end
def params_for_interaction
params.require(:build_item_interaction).permit(:build_item_id, :interaction_type_id, :badge, :data)
end
end
这里的表单代码的样子:
<h2><%= @interaction_info[:description] %> Scan for <%= @build_item.serial_number%></h2>
<p><%= link_to("Back to Unit Record", @build_item) %></p>
<div class="well">
<%= simple_form_for(@interaction,
:url => build_item_interactions_path(type: "end_of_line", serial_number: @build_item.serial_number),
:html => { :class => "form-horizontal" },
wrapper: :horizontal_form,
wrapper_mappings: {
check_boxes: :horizontal_radio_and_checkboxes,
radio_buttons: :horizontal_radio_and_checkboxes,
file: :horizontal_file_input,
boolean: :horizontal_boolean}) do |f| %>
<%= f.association :build_item, as: :hidden %>
<%= f.association :interaction_type, as: :hidden %>
<%= f.input :data, label: "Comments", as: :text %>
<div class="form-group select required build_item_interaction_badge">
<label class="select required col-sm-3 control-label" for="build_item_interaction_badge">
<abbr title="required">*</abbr>Badge
</label>
<div class="col-sm-9">
<%= password_field "build_item_interaction[badge]", nil, class: "string required form-control",
id: "build_item_interaction_badge",
placeholder: "XXXXXXXXXXXX" %>
</div>
</div>
<div class="text-center">
<%= f.submit "Save Checkpoint", data: {disable_with: "Saving..."} %>
</div>
<% end %>
</div>
我想不通,为什么我最终得到2倍POST请求。我尝试过的一些事情:
1)向提交按钮添加数据:{disable_with:“Saving ...”}。这似乎工作(它会灰色/禁用按钮)点击。
2)我已经阅读了CSRF令牌。看起来这个令牌的存在应该足以防止第二个请求被处理。唉 - 日志显示这两个请求都在0.1s内使用相同的authentication_token命中,我得到两条新记录。
有什么建议吗?
更新: 我检查了nginx的访问日志,并有多个请求通过nginx的被处理,以及:
192.168.45.105 - - [14/Feb/2017:13:27:49 -0600] "GET /build_item_interactions/new?serial_number=1702R022&type=end_of_line HTTP/1.1" 499 0 "http://fbdbms/build_items/4501" "Mozilla/5.0 (Windows NT 6
.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-"
192.168.45.105 - - [14/Feb/2017:13:27:49 -0600] "GET /build_item_interactions/new?serial_number=1702R022&type=end_of_line HTTP/1.1" 200 1657 "http://fbdbms/build_items/4501" "Mozilla/5.0 (Windows N
T 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-"
192.168.45.105 - - [14/Feb/2017:13:27:49 -0600] "GET /build_item_interactions/new?serial_number=1702R022&type=end_of_line HTTP/1.1" 200 1657 "http://fbdbms/build_items/4501" "Mozilla/5.0 (Windows N
T 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-"
192.168.45.105 - - [14/Feb/2017:13:27:59 -0600] "POST /build_item_interactions?serial_number=1702R022&type=end_of_line HTTP/1.1" 302 107 "http://fbdbms/build_item_interactions/new?serial_number=170
2R022&type=end_of_line" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-"
192.168.45.105 - - [14/Feb/2017:13:27:59 -0600] "POST /build_item_interactions?serial_number=1702R022&type=end_of_line HTTP/1.1" 302 107 "http://fbdbms/build_item_interactions/new?serial_number=170
2R022&type=end_of_line" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-"
的多个GET请求可能链接到Chrome的预测浏览功能prev discussion here。这很好,但多个POST是问题。
@Gowtham嗨客的配置做的,它看起来像Chrome浏览器确实使多个请求。我已经用nginx日志更新了主帖。它看起来像问题是与[在Chrome浏览预测(http://stackoverflow.com/questions/4460661/what-to-do-with-chrome-sending-extra-requests)。这解释了GET请求,但不是多个POST。 –
Hi @GaryP你有任何提交此表单的JavaScript代码,或者它是一个常规的浏览器表单提交。如果这是一个JS提交,那么你需要确保你通过在JS表单提交事件处理程序中调用event.preventDefault(true)来禁止默认的浏览器提交,否则会导致双提交。 – Gowtham