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

标签 jquery ruby-on-rails

我陷入了一个似乎在论坛(包括 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/

相关文章:

javascript - 仅针对选定元素的 css 样式

javascript - 立即将 csv 文件中的数据显示到 handontable

javascript - SlickGrid 选择编辑器

ruby-on-rails - 有没有办法从 Rails 控制台查看方法的源代码?

ruby-on-rails - Rails 登录重置 session

ruby-on-rails - Ruby on Rails 教程 - 5.26 - Sublime Text "Unable to Save"新文件 "spec/support/utilities.rb"

jquery - 不允许我的 Jquery 代码执行的链接

javascript - 通过单击将标签的文本添加到输入标签

ruby-on-rails - 如何正确扩展 Devise Recoverable?

ruby-on-rails - Prawn :带有模型特定信息的静态头文件可能吗?