2017-04-12 118 views
2

我被困在論壇(包含的Stackoverflow)中似乎很好記錄的問題中,但沒有任何主題可以幫助我解決問題。 簡而言之,我的遠程調用控制器的所有應用程序都被觸發兩次。我在某處讀到它可能是一個jQuery重複問題,但正如你在application.html.erb中看到的那樣,application.js可能不是,因爲jQuery只出現一次。在其他問題中出現的另一個解決方案是刪除turbolink,但這並未解決我的問題。Rails 4.2.7提交遠程表單的事件觸發兩次

我正面臨的錯誤示例: 我有一個使用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="<%[email protected]_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

從你提供的代碼片段看來,它看起來並不像你的動作觸發兩次,而是你的日誌條目被重複。

rails_12factor是可能的罪魁禍首在這裏,它看起來像你有它在你的Gemfile列出兩次。這會將你的日誌輸出到標準輸出和Rails記錄器,這樣他們將會出現在你的終端兩次。

刪除gem "rails_12factor"並保留gem 'rails_12factor', group: :production。然後bundle並重新啓動Rails服務器,問題應該消失。

+0

您是對的,謝謝! –

0

disable_with屬性不幫?

+0

你可以給我一個例子,我可以把它放在我的代碼中嗎?謝謝! –

+0

<%= f.submit「Criar」,:class =>「btn btn-primary」,:data => {:disable_with =>「Please wait ...」}%> – Hristo

+0

它沒有解決我的問題: ( –