gpt4 book ai didi

jquery - Rails 4.2.7提交远程表单的事件触发两次

转载 作者:行者123 更新时间:2023-12-01 04:01:10 24 4
gpt4 key购买 nike

我陷入了一个似乎在论坛(包括 Stackoverflow)上有详细记录的问题,但这些主题都无法帮助我解决问题。简而言之,在整个应用程序中,我对 Controller 的远程调用被触发两次。我在某处读到这可能是 jQuery 重复问题,但正如您在我的 application.html.erb 中看到的,而 application.js 可能不是,因为 jQuery 出现了一次。其他问题中出现的另一个解决方案是删除涡轮链接,但这并没有解决我的问题。

我面临的错误示例:我有一个使用 simple_form 呈现的表单,它向操作提交 GET 请求。

         <%= simple_form_for :create_answer, url: create_answer_path, remote: true do |f| %>
<b>Intenção</b>
<%= f.input :intents, :class=> "form-control", label: false%>

<b>Entidades</b>
<%= f.input :entities, :class=> "form-control", label: false%>

<b>Valores</b>
<%= f.input :values, :class=> "form-control", label: false%>

<b>Text</b>
<%= f.text_area :text, :class=> "form-control", label: false%>

<p class="text-center" style="margin-top:5px;">
<%= f.submit "Criar", :class => "btn btn-primary" %>
</p>
<% end %>

当用户单击“提交”时,操作会被触发两次,正如您在控制台日志中看到的那样: enter image description here由于我在此操作上处理付费远程 API,因此调用它两次是 Not Acceptable 。

这是我的 application.html.erb 文件:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<META HTTP-EQUIV="refresh" CONTENT="<%=@seconds_left%>">
<title>S2Chat</title>
<%= csrf_meta_tags %>


<script src="https://js.pusher.com/4.0/pusher.min.js"></script>


<script type="text/javascript">
$(function(){
$("#dropDown").dropdown();
});
</script>

<%= stylesheet_link_tag "application", :media => "all" %>
<%= yield(:page_stylesheet) if content_for?(:page_stylesheet) %>


<style type="text/css">
body{
overflow-x: hidden;
}

.verticalCenter {
min-height: 100%; /* Fallback for browsers do NOT support vh unit */
min-height: 100vh; /* These two lines are counted as one :-) */

display: flex;
align-items: center;
}

.audios{
display: none;
}
</style>

<audio class="audios" id="new_attendance_audio" controls preload="none"> 
<source src="/assets/door_bell.mp3" type="audio/mpeg">
</audio>

<audio class="audios" id="new_message_audio" controls preload="none">
<source src="/assets/message_alert.mp3" type="audio/mpeg">
</audio>

<audio class="audios" id="attendance_finished_audio" controls preload="none">
<source src="/assets/error1.mp3" type="audio/mpeg">
</audio>


<!-- Fixed navbar -->
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/home/index" style="color:white;">S2Chat</a>
</div>
<% if user_signed_in == true%>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li id="dropDown" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" style="color:white;">
<%=cookies.encrypted[:user_fullname]%><b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li class="dropdown-header">Conta</li>
<li>
<% if cookies.encrypted[:user_type] == "attendant"%>
<%= link_to "Sair", delete_session_path, method: :delete, :data => {:confirm => 'Ao deslogar-se, todos os atendimentos que você está envolvido serão automaticamente finalizados. Você tem certeza que deseja sair?'}%>
<%else%>
<%= link_to "Sair", delete_session_path, method: :delete%>
<%end%>
</li>
</ul>
</li>
</ul>
</div>
<% end%>
</div>
</div>


<div>
<%= yield %>
</div>

<!-- Javascripts
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<%= javascript_include_tag "application" %>

这是我的 application.js:

    //= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require_tree .

这是我的 Gemfile:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.7.1'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem "excon"
gem "rails_12factor"
gem 'rails_12factor', group: :production
gem 'simple_form'
gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem 'twitter-bootstrap-rails'
# twitter bootstrap css & javascript toolkit
#gem 'twitter-bootswatch-rails', '~> 3.3.4'
# twitter bootstrap helpers gem, e.g., alerts etc...
#gem 'twitter-bootswatch-rails-helpers'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
source 'https://rails-assets.org' do
gem 'rails-assets-tether', '>= 1.3.3'
end
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end

group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end

有人可以帮助我吗?谢谢

最佳答案

从您提供的代码片段来看,您的操作似乎没有触发两次,而是您的日志条目重复了。

rails_12factor 可能是这里的罪魁祸首,看起来您在 Gemfile 中将其列出了两次。这会将您的日志输出到 stdout 和 Rails 记录器,因此它们会在您的终端中出现两次。

删除gem“rails_12factor”并保留gem“rails_12factor”,组::生产。然后捆绑并重新启动 Rails 服务器,问题就会消失。

关于jquery - Rails 4.2.7提交远程表单的事件触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43378376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com