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

时间:2017-04-12 19:47:40

标签: jquery ruby-on-rails

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

我面临的错误示例: 我有一个使用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,因此将其调用两次将是不可接受的。

这是我的applicatipon.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

任何人都可以帮助我吗?感谢

2 个答案:

答案 0 :(得分:2)

根据您提供的代码段,您的操作看起来不会触发两次,而不是您的日志条目重复。

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

删除gem "rails_12factor"并保留gem 'rails_12factor', group: :production。然后bundle并重新启动Rails服务器,问题应该消失。

答案 1 :(得分:0)

disable_with属性无济于事?